Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to empty stringstream?

People also ask

How do you clear a Stringstream object?

The clear() member function is inherited from ios and is used to clear the error state of the stream, e.g. if a file stream has the error state set to eofbit (end-of-file), then calling clear() will set the error state back to goodbit (no error). For clearing the contents of a stringstream , using: m. str("");

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 .

Is Stringstream deprecated?

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

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.


I've always done:

s.clear();//clear any bits set
s.str(std::string());

@litb gets into more detail about how to seekp to the start of the stream combined with std::ends you can keep your allocated size.