Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resetting a stringstream [duplicate]

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 
like image 514
user974967 Avatar asked Oct 01 '11 23:10

user974967


People also ask

How do you clear a Stringstream?

For clearing the contents of a stringstream , using: m. str("");

Can you return a Stringstream?

You can't return a stream from a function by value, because that implies you'd have to copy the stream.

Is Stringstream deprecated?

strstream has been deprecated since C++98, std::stringstream and boost::iostreams::array are the recommended replacements.

How do you check if a Stringstream is empty?

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 .


1 Answers

This is the way I usually do it:

ss.str(""); ss.clear(); // Clear state flags. 
like image 178
Darcy Rayner Avatar answered Sep 29 '22 22:09

Darcy Rayner