Stream buffers are input or output devices and provide a low level interface for raw input / output on this device. Streams, on the other hand, provide a higher-level shell around the buffer through basic unformatted I / O functions, and especially through formatted I / O functions (i.e. operator<< and operator>> Overloads). Stream objects can also control the length of the stream buffer.
For example, a file stream has an internal file stream buffer. The stream controls the lifetime of the buffer, and the buffer is what provides the actual read and write capabilities of the file. Stream formatting operators ultimately gain access to the unformatted stream I / O functions, so you always need to use the stream I / O functions and do not directly touch the buffer I / O functions.
Another way to understand the differences is to look at the different uses that they use for local objects. Streams use faces that are related to formatting, such as numpunct and num_get . You can also expect that operator<< and operator>> thread overloads for custom time or money data types will use time and money formatting graphs. However, stream buffers use codecvt graphs to convert between the units used by their interface and bytes. So, for example, the interface for basic_streambuf<char16_t> uses char16_t and therefore basic_streambuf<char16_t> uses codecvt<char16_t, char, mbstate_t> to convert formatted char16_t units written to the buffer to char units written to the base device. Thus, you can see that streams are mainly intended for formatting and stream buffers, provide a low-level interface for unformatted input or output to devices that can use other external encoding.
You can use a stream buffer if you want only unformatted access to an I / O device. You can also use stream buffers if you want to configure multiple streams that share a stream buffer (although you will have to carefully control the buffer lifetime). There will also be special stream streams that you can use, such as wbuffer_convert in C ++ 11, which acts as a facade for basic_streambuf<char> so that it looks like a buffer with a wide character stream. It uses the codecvt face with which it is constructed, instead of using the codecvt face bound to any language. Usually you can achieve the same effect by simply using a broadband buffer pierced by a locale that has a matching face.
bames53 Nov 14 2018-11-11T00: 00Z
source share