开发者

how to clear the content in the String stream.? [duplicate]

开发者 https://www.devze.com 2023-03-26 00:03 出处:网络
This question already has answers here:开发者_StackOverflow社区 How do you clear a stringstream variable?
This question already has answers here: 开发者_StackOverflow社区 How do you clear a stringstream variable? (9 answers) Closed 9 years ago.

can any one tell me how to clear the content of the stringstream..? i tried the following but it didnt work.

stringstream ss;
ss<<"bala"<<"murugan";
cout<<ss.str();         //Output balamurugan
ss.str().clear();
ss<<" hi";
cout<<ss.str();
// output is balamurugan hi 

my required output is " hi" alone.. Pls tell me


What you're doing is clearing the string that is returned from the stringstream, not the internal string. You'll have to actually do the ss.str("")

See here: How do you clear a stringstream variable?


The "clear()" member function is inherited from ios and is used to clear the error state of the stream.

For clearing the contents of a stringstream, using:

stringstreamObject.str("");


Do this:

ss.str("");

This makes the stream empty!

0

精彩评论

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