我有一个包含以下行的文件:
25 1 0 0 0 0
27 1 0 0 0 0
20 0 0 0 0 0
32 1 0 0 0 0
23 1 0 0 0 0
16 0 0 0 0 0
28 1 0 0 0 0
首先,我将第二列中 value=1 的每一行存储为字符串数组的元素。我通过 for 循环调用存储的元素,并将元素的内容解析为整数。如何在 C++ 中做到这一点?
#include <vector>
#include <iostream>
#include <string>
using namespace std;
.....
.....
.....
.....
char line[100];
vector<string> vec_line;
string Vline;
int trimVal, compa, nil0, nil1, nil2, nil3;
......
......
......
//then store lines as strings in vec_line as required by the premise above
// after that, retrieve the contents of the elements of the vector
for (int iline=0; iline<vec_line.size(); iline++) {
cout<<"iline "<<iline<<" vec_line[iline]";
//then I want to parse contents of each string element and store them integer formats
sscanf(vec_line[iline], "%d %d %d %d %d %d", &trimVal, &compa, &nil0, &nil1, &nil2, &nil3); //how would one do this simple parsing task in c and c++?
}
谢谢你。