本文介绍了“内部"与“内部"之间的区别与“相关"流缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

并在 http://www.cplusplus.com/reference/fstream/ifstream/rdbuf/:

但是请注意,这不一定与当前关联的流缓冲区(由ios :: rdbuf返回)相同.

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

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

So what is the internal buffer for if it's not used for input and output operations? And if it means that these two lines could return two different objects, why could this be useful?

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

推荐答案

内部缓冲区用于输入和输出操作,直到使用其他自变量调用 rdbuf 为止.然后就坐在那里.

The internal buffer is used for input and output operations up until the point of calling rdbuf with some other argument. Then it just sits there.

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

You can always re-enable it by calling

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

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

Note that the internal buffer for e.g. std::fstream is always an std::filebuf object owned and managed by the stream object, while the associated buffer can be any streambuf derived object. The stream only stores a base class pointer to it and doesn't manage its lifetime.

还要注意,术语内部缓冲器"是指内部缓冲器".未由标准使用.该标准使用了一些不同的术语:

Also note that the term "internal buffer" is not used by the standard. The standard uses a bit different terminology:

sb filebuf 对象.

维护的数据"以上就是cplusplus.com所谓的内部流缓冲区".

The "maintained data" above is what cplusplus.com calls "the internal stream buffer".

这篇关于“内部"与“内部"之间的区别与“相关"流缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:11