Reading from line to line

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 "" , .

+3
3

>>.

while (ss >> t) {
    lis.push_back(t);
}

. ss << s "1 2 3 4" .

:

""

1- ss << s:

"1 2 3 4"

1- ss >> t:

" 2 3 4"

ss << s:

" 2 3 41 2 3 4"

, 1 2 3 41 2 3 41 2 3 41, s .

+6

s.length() , , . , timrau, stringstream .

stringstream ss;
string s("1 2 3 4 5 6 7 8");
ss << s;
double t;
list<double> lis;
while (ss >> t) {
    lis.push_back(t);
}

for(auto e : lis){
    cout << e << " ";
}
0

fooobar.com/questions/467 / ... contains an example of a boost tokenizer . You may want to label your line and repeat it this way. This will solve the problem of the lack of finite space timrau .

0
source

All Articles