Giampiero Gabbiani wrote:
Is it possible to reset a std::stringstream in order to reuse it once more?
I tried with flush() method without any success...
Thanks in advance
Giampiero
The "str()" and "str( string )" methods should work.
However, on some platforms these may not work so you
may use the ugly reconstruct hack.
#include <sstream>
#include <iostream>
int main()
{
std::istringstream iss( "Hi there" );
std::ostringstream oss;
std::string foo;
iss >> foo;
oss << foo;
iss.str( "boo hoo" ); // Set the input to new string
iss >> foo;
oss << foo;
std::cout << oss.str() << "\n";
// Another way is to reconstruct the istringstream
// UGLY but some implementations have bugs and this is the
// only way.
iss.~istringstream();
new ( (void *) &iss ) std::istringstream( "Reconstruct this" );
oss.str( "" ); // reset the output string.
iss >> foo;
oss << foo;
std::cout << oss.str() << "\n";
// Another way is to reconstruct the ostringstream
oss.~ostringstream();
new ( (void *) &oss ) std::ostringstream;
iss >> foo;
oss << foo;
std::cout << oss.str() << "\n";
}