hello我正在读取CSV文件中的数据,并已成功解析出数据中的所有字符,现在我需要将每列中的字符串数据转换为适当的类型(int,double等)。我有我想要的以下类型声明,然后从那里每个变量应该更新为尽可能多的行,所以我可以采取该变量并将其称为程序中的其他地方。请参阅下面解析向量中字符串的数据为适当的数据类型C++
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
}
您似乎缺少提问? –
我试图将数据完全解析后将其转换为我在上面声明的占用数据类型? – looneytunes24
你有什么问题呢?通常在使用CSV文件时,必须在解析文件之前决定列类型。 –