开发者

How to clear stringstream? [duplicate]

开发者 https://www.devze.com 2022-12-31 02:10 出处:网络
This question already has answers here: How do you clear a stringstream variable? (9 answers) 开发者_StackOverflow
This question already has answers here: How do you clear a stringstream variable? (9 answers) 开发者_StackOverflow Closed 7 years ago.
stringstream parser;

parser << 5;
short top = 0;
parser >> top;
parser.str(""); //HERE I'M RESETTING parser

parser << 6; //DOESN'T PUT 6 INTO parser
short bottom = 0;
parser >> bottom;

Why doesn't it work?


Typically to 'reset' a stringstream you need to both reset the underlying sequence to an empty string with str and to clear any fail and eof flags with clear.

parser.str( std::string() );
parser.clear();

Typically what happens is that the first >> reaches the end of the string and sets the eof bit, although it successfully parses the first short. Operations on the stream after this immediately fail because the stream's eof bit is still set.

0

精彩评论

暂无评论...
验证码 换一张
取 消