resetting a stringstream

≯℡__Kan透↙ 提交于 2019-11-27 19:12:48

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:

  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.

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!