在Scala中,可以通过//read in the file into a double vector
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <sstream>
using namespace std;
class OBJs{
public:
string Obj;
int numItemsInObj;
};
//vector to store all of the data for the grid and the row of the big vector
vector<OBJs> row;
vector<vector<OBJs>> grid;
//maybe create an object for the found parameters
OBJs tempObj;
//functions
void load(string fileName);
void printObjs();
int main(int argc, const char * argv[]) {
string file;
cout << "Please enter your filename here: ";
cin >> file;
load(file);
printObjs();
return 0;
}
void load(string fileName){
//read in a line and insert the line(string) into the second index of the vector
string line;
string zeroOrOne;
ifstream file;
file.open(fileName);
//basically a guard let function in swift
if(!file){
cout << "Could not open the file " << fileName << endl;
//exit(0);
}
// you have successfully entered the file specified
while(getline(file, line)){
//grabbing each line of the file into the var line
//delimit this line by , in getline
istringstream ss(line);
while(getline(ss, zeroOrOne, ',')){
//put it into the row vector.
tempObj.Obj = zeroOrOne;
tempObj.numItemsInObj = 0;
row.push_back(tempObj);
}
//put the row in the grid.
grid.push_back(row);
}
file.close();
}
void printObjs(){
for(int i =0; i < grid.size(); i++){
for(int j =0; j< row.size(); j++){
if(grid[i][j].Obj == ""){
//cout << "nothing in this element" << endl;
} else {
cout << grid[i][j].Obj;
}
}
cout << endl;
}
}
访问类或特征中的类型定义。可以通过Class#Type
访问对象内部的类型定义。
使用一个对另一个的用例,注意事项和陷阱有哪些?
对于具有相同类型参数的许多不相关的类,可以使用什么好方法?例如:
Object.Type
如何避免重复类型参数限制和重用代码?