When I try to parse whitespace separator values from a string, I found this curious behavior that the string is read cyclically.
Here is the program:
stringstream ss;
string s("1 2 3 4");
double t;
list<double> lis;
for(int j=0; j!=10; ++j){
ss << s;
ss >> t;
lis.push_back(t);
}
for(auto e : lis){
cout << e << " ";
}
Here's the conclusion:
1 2 3 41 2 3 41 2 3 41
If I add a finite space like s= "1 2 3 4 ";, I get
1 2 3 4 1 2 3 4 1 2
Now the questions:
1) If I do not know how many entries are in line s, how did I read everything in the list?
2) which operator<<am I really calling in ss << s;? Is this indicated for reading in a circle?
3) Can I better understand parsing?
Thanks already!
Here's the fixed code (thanks to timrau ):
// declarations as before
ss << s;
while(ss >> t){
lis.push_back(t);
}
// output as before
This gives:
1 2 3 4
optional. (Remember to clear stringstreamfrom ss.clear()before processing the next input .;))
HeywoodFloyd: boost/tokenizer "" , .