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
For clearing the contents of a stringstream , using: m. str("");
You can't return a stream from a function by value, because that implies you'd have to copy the stream.
strstream has been deprecated since C++98, std::stringstream and boost::iostreams::array are the recommended replacements.
myStream. rdbuf()->in_avail() can be used to get the count of available characters ready to be read in from a stringstream , you can use that to check if your stringstream is "empty." I'm assuming you're not actually trying to check for the value null .
This is the way I usually do it:
ss.str(""); ss.clear(); // Clear state flags.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With