How to clear an instance of std :: ostringstream so that it can be reused?

Possible duplicate:
How to reuse ostringstream?

I used std::ostringstreamto transform the values floatand intin line, but I still can not reuse copy. To illustrate what I mean here, the following along with the methods that I 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----****

, ostringstream, . , clear() flush() , , ? http://www.cplusplus.com/reference/iostream/ostringstream/, , , . reset ?

+5
2

stream.str("");, . , . ++ , , , , :

{
    std::ostringstream oss;
    oss << 10;
    std::cout << oss.str();
}

{
    std::ostringstream oss;
    oss << 20.5;
    std::cout << oss.str();
}

:

std::cout << static_cast<std::ostringstream&>(std::ostringstream() << 10).str();
+18

clear() . . str() :

stream.str("");

, .

+10

All Articles