简体   繁体   中英

Weird stringstream behavior when extracting and inserting multiple times.

Can someone help me understand this stringstream behavior?

stringstream temp;
temp << "342 1 ";
int a;
while (temp >> a) {
    cout << a << endl;
}
temp << "56" << " ";
temp >> a;
cout << a << endl;

Which outputs:

342
1
1

I would expect it to output

342
1
56

This was compiled in visual studios 2015.

After you have read the value 1, the next while will reach end-of-file and put the stream in an error state. Any further reads will then fail, and leave a unchanged.

You can clear the error state by calling temp.clear() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM