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,259
  • 8
  • 33
  • 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,793
  • 1
  • 19
  • 22
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,913
  • 9
  • 31
  • 50
2

Do this:

ss.str("");

This makes the stream empty!

Nawaz
  • 353,942
  • 115
  • 666
  • 851