how to reset stringstream object

If you want a new ostringstream object each time through the loop, the obvious solution is to declare a new one at the top of the loop. All of the ostream types contain a lot of state, and depending on context, it may be more or less difficult to reset all of the state.


If you want to replace the contents of the stringstream with something else, you can do that using the str() method. If you call it without any arguments it will just get the contents (as you're already doing). However, if you pass in a string then it will set the contents, discarding whatever it contained before.

E.g.:

std::stringstream os;
os.str("some text for the stream");

For more information, check out the method's documentation: http://www.cplusplus.com/reference/sstream/stringstream/str