为什么不允许复制字符串流?

发布于 2024-11-07 13:05:23 字数 191 浏览 2 评论 0原文

int main()
{
   std::stringstream s1("This is my string.");
   std::stringstream s2 = s1; // error, copying not allowed
}

我找不到无法复制 stringstream 的原因。你能提供一些参考吗?

int main()
{
   std::stringstream s1("This is my string.");
   std::stringstream s2 = s1; // error, copying not allowed
}

I couldn't find a reason as to why i can't copy stringstream. could you provide some reference?

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

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

发布评论

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

评论(3

寄与心 2024-11-14 13:05:23

通过将复制构造函数设置为私有,可以禁用 C++ 中任何流的复制。

Any 表示 ANY,无论是 stringstream、istream、ostream、iostream 还是其他。

复制被禁用,因为它没有意义。了解stream的含义以及真正理解为什么复制stream没有意义非常非常重要。 stream 不是一个可以复制的容器。它不包含数据。

如果列表/向量/地图或任何容器是一个桶,那么流就是数据流经的软管。将流视为您获取数据的某个管道;管道 - 一侧是源(发送器),另一侧是接收器(接收器)。这就是所谓的单向流。还有双向流,数据可以在两个方向上流动。那么复制这样的东西有什么意义呢?它根本不包含任何数据。 通过您可以获取数据。

现在假设一段时间,如果允许复制流,并且您创建了 std::cin副本,它实际上是输入流。假设复制的对象是copy_cin。现在问问自己:当已经从 std::cin 读取了完全相同的数据时,从 copy_cin 流读取数据是否有意义。不,它不会使从某种意义上说,因为用户只输入一次数据,键盘(或输入设备)只生成一次电信号,并且它们只流过所有其他硬件和低级 API 一次。你的程序如何读取它两次或更多次

因此,不允许创建复制,但允许创建引用

std::istream  copy_cin = std::cin; //error
std::istream & ref_cin = std::cin; //ok

另请注意,您可以创建流的另一个实例,并可以使其使用相同的底层缓冲区 旧流当前正在使用。请参阅:https://ideone.com/rijov

Copying of ANY stream in C++ is disabled by having made the copy constructor private.

Any means ANY, whether it is stringstream, istream, ostream,iostream or whatever.

Copying of stream is disabled because it doesn't make sense. Its very very very important to understand what stream means, to actually understand why copying stream does not make sense. stream is not a container that you can make copy of. It doesn't contain data.

If a list/vector/map or any container is a bucket, then stream is a hose through which data flows. Think of stream as some pipe through which you get data; a pipe - at one side is the source (sender), on the other side is the sink (receiver). That is called unidirectional stream. There're also bidirectional streams through which data flows in both direction. So what does it make sense making a copy of such a thing? It doesn't contain any data at all. It is through which you get data.

Now suppose for a while if making a copy of stream is allowed, and you created a copy of std::cin which is in fact input stream. Say the copied object is copy_cin. Now ask yourself : does it make sense to read data from copy_cin stream when the very same data has already been read from std::cin. No, it doesn't make sense, because the user entered the data only once, the keyboard (or the input device) generated the electric signals only once and they flowed through all other hardwares and low-level APIs only once. How can your program read it twice or more?

Hence, creating copy is not allowed, but creating reference is allowed:

std::istream  copy_cin = std::cin; //error
std::istream & ref_cin = std::cin; //ok

Also note that you can create another instance of stream and can make it use the same underlying buffer which the old stream is currently using. See this : https://ideone.com/rijov

眉黛浅 2024-11-14 13:05:23

要直接回答这个问题,您不能复制,因为 stringstream 类的复制构造函数被声明为私有。

它可能是这样声明的,因为在大多数情况下复制流似乎很尴尬,因此没有一个流类具有公共复制构造函数。

To directly answer the question, you can't copy because the copy constructor for the stringstream class is declared as private.

It was probably declared that way because it seems awkward to copy a stream in most cases, so none of the stream classes have public copy constructors.

孤千羽 2024-11-14 13:05:23

如上所述,您无法复制流,但如果需要,您可以复制数据:

std::stringstream from;
std::stringstream to;

std::copy(std::istream_iterator<char>(from), std::istream_iterator<char>(),
          std::ostream_iterator<char>(to));

As mentioned above you cannot copy stream, but if you need you could copy data:

std::stringstream from;
std::stringstream to;

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