重置字符串流
如何将字符串流的状态“重置”为创建它时的状态?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我通常这样做的方式:
This is the way I usually do it:
我会做
编辑:修复 christianparpart 和 Nemo 报告的错误。谢谢。
PS:上面的代码在堆栈上创建了一个新的 stringstream 对象,并将 ss 中的所有内容与新对象中的内容交换。
优点:
ss
现在处于全新状态。ss
内部数据重置为初始状态一样。更多:
与赋值运算符相比:在新对象在堆中具有已分配缓冲区的情况下,STL 交换方法可以比赋值运算符更快。在这种情况下,赋值运算符必须为新对象分配缓冲区,然后可能需要为旧对象分配另一个缓冲区,然后将数据从新对象的缓冲区复制到旧对象的新缓冲区。实现快速交换非常容易,例如仅交换缓冲区的指针。
C++11。我见过一些移动赋值运算符的实现比交换慢,尽管可以修复,但 STL 开发人员可能不希望留下带有大量数据的移动对象
std::move( )
不保证移动的对象被清空。return std::move(m_container);
不会清除 m_container。所以你必须这样做自动 to_return(std::move(m_container));
m_container.clear();
return to_return;
这再好不过了,
因为后者保证它不会复制缓冲区。
所以我总是更喜欢
swap()
只要它合适。I would do
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:
ss
will now be in a fresh-new state.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
because the latter guarantees it won't copy buffers.
So I always prefer
swap()
as long as it fits.基于上面的答案,我们还需要重置所有格式。总之,当构造新的 std::stringstream 实例时,我们将缓冲区内容、流状态标志和任何格式重置为其默认值。
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.