resetting a stringstream

user974967 picture user974967 · Oct 2, 2011 · Viewed 62.7k times · Source

How do I "reset" the state of a stringstream to what it was when I created it?

int firstValue = 1;
int secondValue = 2;

std::wstringstream ss;

ss << "Hello: " << firstValue;

std::wstring firstText(ss.str());

//print the value of firstText here


//How do I "reset" the stringstream here?
//I would like it behave as if I had created
// stringstream ss2 and used it below.


ss << "Bye: " << secondValue;

std::wstring secondText(ss.str());

//print the value of secondText here

Answer

Darcy Rayner picture Darcy Rayner · Oct 2, 2011

This is the way I usually do it:

ss.str("");
ss.clear(); // Clear state flags.