resetting a stringstream

前端 未结 3 1397
温柔的废话
温柔的废话 2020-12-02 15:11

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 <<         


        
相关标签:
3条回答
  • 2020-12-02 15:39

    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);
    }
    
    0 讨论(0)
  • 2020-12-02 15:40

    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.

    0 讨论(0)
  • 2020-12-02 15:51

    This is the way I usually do it:

    ss.str("");
    ss.clear(); // Clear state flags.
    
    0 讨论(0)
提交回复
热议问题