How do you clear a std::ostringstream instance so it can be reused?

Viewed 13472

Possible Duplicate:
How to reuse an ostringstream?

I have been using std::ostringstream to convert float and int values to strings but I cannot find anyway to reuse an instance. To illustrate what I mean here is the following along with the methods that I have tried to use to clear the stream

 #include <iostream>
 #include <sstream>
 using namespace std;

 int main() {
   ostringstream stream;
   stream << "Test";
   cout << stream.str() << endl;  
   stream.flush();                
   stream << "----";
   cout << stream.str() << endl; 
   stream.clear();
   stream << "****";
   cout << stream.str() << endl;
   return 0;
 }

generates output

 Test
 Test----
 Test----****

This is giving me a problem as I am having to create many instances of ostringstream which is wasteful. Clearly clear() and flush() do not do what I need so is there a way to do this? I checked the documentation at http://www.cplusplus.com/reference/iostream/ostringstream/ but there is nothing there that appears to do what I need. Is there a way to reset or clear the stream??

2 Answers
Related