5

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

Balamurugan
  • 2,139
  • 8
  • 31
  • 48

3 Answers3

12

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?

Community
  • 1
  • 1
Seb Holzapfel
  • 3,673
  • 1
  • 18
  • 21
4

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("");
nirmus
  • 4,733
  • 8
  • 29
  • 50
2

Do this:

ss.str("");

This makes the stream empty!

Nawaz
  • 341,464
  • 111
  • 648
  • 831