C++ 重置字符串流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7623650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:14:02  来源:igfitidea点击:

resetting a stringstream

c++stringstringstream

提问by user974967

How do I "reset" the state of a stringstream to what it was when I created it?

如何将 stringstream 的状态“重置”到我创建它时的状态?

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

回答by Darcy Rayner

This is the way I usually do it:

这是我通常的做法:

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

回答by Yong

I would do

我会做

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

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

编辑:修复了 christianparpart 和 Nemo 报告的错误。谢谢。

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

PS:上面的代码在堆栈上创建了一个新的 stringstream 对象,并将所有内容ss与新对象中的内容交换。

Advantages:

好处:

  1. It guarantees sswill 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 ssinternal data to initial state.
  1. 它保证ss现在将处于全新状态。
  2. 新对象在堆栈上内联创建,以便编译器可以轻松优化代码。最后,就像将所有ss内部数据重置为初始状态一样。

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;

  1. 与赋值运算符相比:在新对象在堆中具有已分配缓冲区的情况下,STL 交换方法可能比赋值运算符更快。在这种情况下,赋值运算符必须为新对象分配缓冲区,然后可能需要为旧对象分配另一个缓冲区,然后将数据从新对象的缓冲区复制到旧对象的新缓冲区。实现快速交换非常容易,例如只交换缓冲区的指针。

  2. C++11。我已经看到了一些比交换慢的移动赋值运算符的实现,虽然可以修复,但可能 STL 开发人员不希望移动对象留下大量数据

  3. std::move()不保证移动的对象被清空。return std::move(m_container);不清除 m_container。所以你将不得不做

    自动 to_return(std::move(m_container)); m_container.clear(); 返回_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.

所以我总是喜欢swap()只要它适合。

回答by GameSalutes

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.

基于上面的答案,我们还需要重置任何格式。总之,当构造新的 std::stringstream 实例时,我们将缓冲区内容、流状态标志和任何格式重置为它们的默认值。

void reset(std::strinstream& stream)
{
    const static std::stringstream initial;

    stream.str(std::string());
    stream.clear();
    stream.copyfmt(initial);
}