gpt4 book ai didi

c++ - "internal"与 "associated"流缓冲区之间的区别

转载 作者:可可西里 更新时间:2023-11-01 17:58:11 26 4
gpt4 key购买 nike

来自 http://www.cplusplus.com/reference/ios/ios/rdbuf/ :

Some derived stream classes (such as stringstream and fstream) maintain their own internal stream buffer, to which they are associated on construction. Calling this function to change the associated stream buffer shall have no effect on that internal stream buffer: the stream will have an associated stream buffer which is different from its internal stream buffer (although input/output operations on streams always use the associated stream buffer, as returned by this member function).

然后 http://www.cplusplus.com/reference/fstream/ifstream/rdbuf/ :

Returns a pointer to the internal filebuf object.

Notice however, that this is not necessarily the same as the currently associated stream buffer (returned by ios::rdbuf).

那么如果不用于输入输出操作,内部缓冲区是干什么用的呢?如果这意味着这两行可以返回两个不同的对象,那为什么会有用?

std::stringstream ss;
ss.rdbuf(); // (1) returns "internal" stream buffer?
static_cast<std::ios&>(ss).rdbuf(); // (2) returns "associated" stream buffer?

最佳答案

内部缓冲区用于输入和输出操作,直到调用 rdbuf 为止。与其他一些论点。然后它就坐在那里。

您随时可以通过调用重新启用它

stream.basic_ios::rdbuf(stream.rdbuf());

请注意内部缓冲区,例如std::fstream始终是 std::filebuf由流对象拥有和管理的对象,而关联的缓冲区可以是任何 streambuf派生对象。流仅存储指向它的基类指针,不管理其生命周期。

另请注意,标准未使用术语“内部缓冲区”。该标准使用了一些不同的术语:

The class basic_ifstream<charT, traits> supports reading from named files. It uses a basic_filebuf<charT, traits> object to control the associated sequence. For the sake of exposition, the maintained data is presented here as:

sb, the filebuf object.

上面的“维护数据”就是cplusplus.com所说的“内部流缓冲区”。

关于c++ - "internal"与 "associated"流缓冲区之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49591500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com