重置字符串流

发布于 2024-12-07 21:17:16 字数 483 浏览 0 评论 0原文

如何将字符串流的状态“重置”为创建它时的状态?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2024-12-14 21:17:16

这是我通常这样做的方式:

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

This is the way I usually do it:

ss.str("");
ss.clear(); // Clear state flags.
孤独患者 2024-12-14 21:17:16

我会做

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

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

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

优点:

  1. 它保证ss现在处于全新状态。
  2. 新对象是内联并在堆栈上创建的,以便编译器可以轻松优化代码。最后,就像将所有ss内部数据重置为初始状态一样。

更多:

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

这再好不过了,

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

因为后者保证它不会复制缓冲区。

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

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.

双手揣兜 2024-12-14 21:17:16

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

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文