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
This is the way I usually do it:
ss.str("");
ss.clear(); // Clear state flags.
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:
- It guarantees
ss
will now be in a fresh-new state. - 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:
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.
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
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 doauto 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.
Building on answer above, we need to also 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::strinstream& stream)
{
const static std::stringstream initial;
stream.str(std::string());
stream.clear();
stream.copyfmt(initial);
}
来源:https://stackoverflow.com/questions/7623650/resetting-a-stringstream