resetting a stringstream

C++StringStringstream

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

This is the way I usually do it:

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

Solution 2 - C++

I would do

std::wstringstream temp;
ss.swap(temp);

Edit: fixed the error reported by christianparpart and Nemo. Thanks.

PS: The above code creates a new stringstream object on the stack and swaps everything in ss with those in the new object.

Advantages:

  1. It guarantees ss will now be in a fresh-new state.
  2. The new object is created inline and on the stack, so that the compiler can easily optimize the code. At the end, it will be like resetting all ss internal data to initial state.

More:

  1. Compared to assignment operator: STL swap methods can be faster than assignment operator in the cases where the new object has an allocated buffer in the heap. In such a case, assignment operator has to allocate the buffer for the new object, then it MAY need to allocate another buffer for the old object, and then copy the data from the new object's buffer to the old object's new buffer. It is very easy to implement a fast swap, which just swaps pointers of the buffers for example.

  2. C++11. I have seen some implementation of move assignment operator that is slower than swap, although that can be fixed, but probably STL developer won't want to leave a moved object with a lot of data

  3. std::move() doesn't guarantee the moved object is emptied. return std::move(m_container); doesn't clear m_container. So you will have to do

    auto to_return(std::move(m_container)); m_container.clear(); return to_return;

Which can't be better than

auto to_return;
m_container.swap(to_return);
return to_return;

because the latter guarantees it won't copy buffers.

So I always prefer swap() as long as it fits.

Solution 3 - C++

Building on answer above, we also need to reset any formatting. In all we are resetting the buffer contents, the stream state flags, and any formatting to their defaults when a new std::stringstream instance is constructed.

void reset(std::stringstream& stream)
{
    const static std::stringstream initial;
    
    stream.str(std::string());
    stream.clear();
    stream.copyfmt(initial);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser974967View Question on Stackoverflow
Solution 1 - C++Darcy RaynerView Answer on Stackoverflow
Solution 2 - C++YongView Answer on Stackoverflow
Solution 3 - C++GameSalutesView Answer on Stackoverflow