C++ 什么是流缓冲?我如何使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8116541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:01:52  来源:igfitidea点击:

What exactly is streambuf? How do I use it?

c++iostream

提问by user541686

I'm trying to learn a bit more about how I/O streams work in C++, and I'm really confused at when to use what.

我正在尝试更多地了解 I/O 流如何在 C++ 中工作,但我真的很困惑何时使用什么。

What exactly is a streambuf?
When do I use a streambuf, as compared to a string, an istream, or a vector? (I already know the last three, but not how streambufcompares to them, if it does at all.)

究竟是什么streambuf?与 a 、 an或 a相比,
我什么时候使用a ?(我已经知道最后三个,但不知道与它们相比如何,如果有的话。)streambufstringistreamvectorstreambuf

采纳答案by bames53

Stream buffers represent input or output devices and provide a low level interface for unformatted I/O to that device. Streams, on the other hand, provide a higher level wrapper around the buffer by way of basic unformatted I/O functions and especially via formatted I/O functions (i.e., operator<<and operator>>overloads). Stream objects may also manage a stream buffer's lifetime.

流缓冲区代表输入或输出设备,并为该设备的未格式化 I/O 提供低级接口。另一方面,流通过基本的无格式 I/O 函数,特别是通过有格式的 I/O 函数(即,operator<<operator>>重载)提供了缓冲区周围的更高级别的包装器。流对象还可以管理流缓冲区的生命周期。

For example a file stream has an internal file stream buffer. The stream manages the lifetime of the buffer and the buffer is what provides actual read and write capabilities to a file. The stream's formatting operators ultimately access the stream buffer's unformatted I/O functions, so you only ever have to use the stream's I/O functions, and don't need to touch the buffer's I/O functions directly.

例如,文件流具有内部文件流缓冲区。流管理缓冲区的生命周期,缓冲区为文件提供实际的读写能力。流的格式化操作符最终会访问流缓冲区的未格式化 I/O 函数,因此您只需使用流的 I/O 函数,而无需直接接触缓冲区的 I/O 函数。

Another way to understand the differences is to look at the different uses they make of locale objects. Streams use the facets that have to do with formatting such as numpunctand num_get. You can also expect that the overloads of stream operator<<and operator>>for custom time or money data types will use the time and money formatting facets. Stream buffers, however, use the codecvt facets in order to convert between the units their interface uses and bytes. So, for example, the interface for basic_streambuf<char16_t>uses char16_tand so basic_streambuf<char16_t>internally uses codecvt<char16_t, char, mbstate_t>by default to convert the formatted char16_tunits written to the buffer to charunits written to the underlying device. So you can see that streams are mostly for formatting and stream buffers provide a low level interface for unformatted input or output to devices which may use a different, external encoding.

另一种理解差异的方法是查看它们对语言环境对象的不同用途。流使用与格式有关的方面,例如numpunctnum_get。您还可以预期流的重载operator<<operator>>自定义时间或金钱数据类型将使用时间和金钱格式化方面。然而,流缓冲区使用 codecvt facet 以便在它们的接口使用的单位和字节之间进行转换。因此,例如,basic_streambuf<char16_t>useschar16_t和 sobasic_streambuf<char16_t>内部codecvt<char16_t, char, mbstate_t>默认使用的接口将char16_t写入缓冲区的格式化单位转换为char写入底层设备的单元。因此,您可以看到流主要用于格式化,流缓冲区为未格式化的输入或输出到可能使用不同外部编码的设备提供低级接口。

You can use a stream buffer when you want only unformatted access to an I/O device. You can also use stream buffers if you want to set up multiple streams that share a stream buffer (though you'll have to carefully manage the lifetime of the buffer). There are also special purpose stream buffers you might want to use, such as wbuffer_convertin C++11 which acts as a fa?ade for a basic_streambuf<char>to make it look like a wide character stream buffer. It uses the codecvt facet it's constructed with instead of using the codecvt facet attached to any locale. You can usually achieve the same effect by simply using a wide stream buffer imbued with a locale that has the appropriate facet.

当您只想对 I/O 设备进行无格式访问时,可以使用流缓冲区。如果要设置共享流缓冲区的多个流,也可以使用流缓冲区(尽管您必须仔细管理缓冲区的生命周期)。还有一些您可能想要使用的特殊用途的流缓冲区,例如wbuffer_convert在 C++11 中,它充当 a 的basic_streambuf<char>外观,使其看起来像一个宽字符流缓冲区。它使用它构建的 codecvt facet,而不是使用附加到任何语言环境的 codecvt facet。您通常可以通过简单地使用带有适当方面的区域设置的宽流缓冲区来实现相同的效果。

回答by COD3BOY

With the help of streambuf, we can work in an even lower level. It allows access to the underlying buffers.

借助streambuf,我们可以在更低的层次上工作。它允许访问底层缓冲区。

Here are some good examples : Copy, load, redirect and tee using C++ streambufsand in reference to comparison, This might be helpful,

这里有一些很好的例子:使用 C++ streambufs 复制、加载、重定向和 tee并参考比较,这可能会有所帮助,

enter image description here

在此处输入图片说明

See this for more details : IOstream Library

有关更多详细信息,请参阅:IOstream 库