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文件時,必須在解析文件之前決定列類型。 –