【发布时间】:2017-03-27 16:27:02
【问题描述】:
大家好,我正在从 CSV 文件中读取数据并成功解析出数据中的所有字符,现在我需要将每列中的字符串数据转换为其适当的类型(int、double 等)。我声明了我想要的以下类型,然后从那里每个变量都应该更新为尽可能多的行,这样我就可以获取该变量并在程序中的其他地方调用它。请看下文
vector<std::vector<std::string> > matrix;
int a;
int b;
int c;
double c;
double d;
double e;
double f;
int f;
double f;
double f;
string line;
ifstream file("mynumbers - Copy.csv");
if (!file.is_open())
perror("error while opening file");
getline(file, line, '\n');//get the first line of columns names out
while (getline(file, line,'\n')) //find the endline charcater
{
vector<string> vec;
string linevec;
istringstream ss(line);//allows for parsing each line
while (getline(ss, linevec, ','))
{
vec.push_back(linevec);//push the parsed data for that line into a vector
}
matrix.emplace_back(vec);//push each parsed line into another vector
}
【问题讨论】:
-
你好像没问问题?
-
我正在尝试将完全解析后的数据转换为我在上面声明的适当数据类型?
-
你这样做有什么问题?通常对于 CSV 文件,您必须在解析文件之前决定列类型。
-
不能根据运行时类型进行转换。您需要一个基于类型调用特定函数的模板函数。
-
int a, b, c,f ;双 d,e,g,h,i,j,k 对不起