I have the following text file:
Jean Rousseau
1001 15.50
Steve Woolston
1002 1423.20
Michele Rousseau
1005 52.75
Pete McBride
1007 500.32
Florence Rousseau
1010 1323.33
Lisa Covi
1009 332.35
Don McBride
1003 12.32
Chris Carroll
1008 32.35
Yolanda Agredano
1004 356.00
Sally Sleeper
1006 32.36
I have to store the strings (Jean Rousseau), ints (1001) and floats (15.50) in 3 std::vectors.
I have a working solution, but would like to know if it's the proper way to do it. My code is the following:
int counter=0;
std::string line;
std::ifstream myfile("InFile.txt");
std::vector<std::string> names;
std::vector<int> ids;
std::vector<float> balances;
int buf_int;
float buf_float;
while(myfile) {
std::getline(myfile, line);
std::cout << line << std::endl;
std::stringstream ss(line);
if(counter%2 == 0) {
names.push_back(line);
}
else {
if(ss >> buf_int) ids.push_back(buf_int);
if(ss >> buf_float) balances.push_back(buf_float);
}
counter++;
}
Please let me know if there is a better way to do it. Thanks.
Aucun commentaire:
Enregistrer un commentaire