How to clear stringstream? [duplicate]

社会主义新天地 提交于 2019-11-26 02:41:47

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2848087/how-to-clear-stringstream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!