How to clear contents in a String. Stream?

can anyone tell me how to clear the contents of the string stream ...? I tried the following, but it did not 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 of "hi" is one. Pls tell me

+5
source share
3 answers

What you do is clear the string that is returned from the string, not the string inside . You really need to doss.str("")

See here: How to clear a stringstream variable?

+10
source

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

, :

stringstreamObject.str("");
+4

:

ss.str("");

!

+2

All Articles