如何复制从一个stringstream对象到另一个在C + +? [英] how copy from one stringstream object to another in C++?

查看:204
本文介绍了如何复制从一个stringstream对象到另一个在C + +?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有stringstream对象ss1

I have stringstream object ss1

现在我想从这个创建另一个副本。

now I would like to create another copy from this one.

我试试这个

std::stringstream ss2 = ss1;

std::stringstream ss2(ss1)

无效

错误消息如下

std :: ios :: basic_ios(const std :: ios&)不能从bsl :: basic_stringstream,bsl :: allocator> :: basic_stringstream(const bsl :: basic_stringstream,bsl :: allocator>&)。

std::ios::basic_ios(const std::ios &) is not accessible from bsl::basic_stringstream, bsl::allocator>::basic_stringstream(const bsl::basic_stringstream, bsl::allocator>&).

推荐答案

确实,流是不可复制的(虽然它们是可移动的)。

Indeed, streams are non-copyable (though they are movable).

根据您的用法,以下工作很好:

Depending on your usage, the following works quite well:

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream ss1;
    ss1 << "some " << 123 << " stuff" << std::flush;

    std::stringstream ss2;
    ss2 << ss1.rdbuf(); // copy everything inside ss1's buffer to ss2's buffer

    std::cout << ss1.str() << std::endl;
    std::cout << ss2.str() << std::endl;
}

输出:

一些123个素材

一些123个素材

some 123 stuff
some 123 stuff

这篇关于如何复制从一个stringstream对象到另一个在C + +?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆