6

I just wanted to know what's the difference between clear() and str("");

For example:

stringstream ss("Stack Overflow");

ss.clear();

ss.str("");

I wanted to know the underlying technical difference.

Priyanka Mishra
  • 10,400
  • 18
  • 62
  • 75

2 Answers2

15

clear() clears the error state flags in the stringstream. That is to say it sets the error state to goodbit(which is equal to zero).

str("") sets the associated string object to the empty string.

They actually do completely different things. The peculiar choice of names only make it sound as though they perform similar tasks.

AlfaZulu
  • 921
  • 5
  • 13
  • 1
    I hardly think the naming is "peculiar" at all, they are not similar in the least. The fact that you think it is confusing perplexes me. – radman Oct 13 '11 at 23:19
5
void clear ( iostate state = goodbit ) //clears and sets error flag passed as parameter

string str ( ) const;          //to get value from string stream
void str ( const string & s ); //to set value to string stream
yesraaj
  • 46,370
  • 69
  • 194
  • 251