简体   繁体   English

我需要一些帮助来理解Nicolai Josuttis的这本书

[英]I need some help to understand this paragraph from Nicolai Josuttis book

I'd like to have some explanation regarding this paragraph on page 518 of Nicolai Josuttis book "The C++ Standard Library" (first edition): 我想在Nicolai Josuttis的书“ The C ++ Standard Library”(第一版)的第518页上对此段进行一些解释:

These flags are maintained by the class basic_ios and are thus present in all objects of type basic_istream or basic_ostream . 这些标志由basic_ios类维护,因此存在于basic_istreambasic_ostream类型的所有对象中。 However, the stream buffers don't have state flags. 但是,流缓冲区没有状态标志。 One stream buffer can be shared by multiple stream objects, so the flags only represent the state of the stream as found in the last operation. 一个流缓冲区可以由多个流对象共享,因此标志仅代表在上一个操作中发现的流状态。 Even this is only the case if goodbit was set prior to this operation. 即使只有在此操作之前设置了好位,也只有这种情况。 Otherwise the flags may have been set by some earlier operation. 否则,这些标志可能已通过某些较早的操作设置。

I don't understand what does he mean by "the stream buffer don't have state flags" and right below this paragraph there's a table with the title "Member functions for stream states". 我不明白他的意思是“流缓冲区没有状态标志”,在此段的正下方有一张标题为“流状态的成员函数”的表。

Streams consist of two objects: 流包含两个对象:

  1. The actual stream object ( std::istream or std::ostream , derived from std::ios ). 实际的流对象( std::istreamstd::ostream ,从std::ios派生)。
  2. The stream buffer, ie, a class derived from std::streambuf . 流缓冲区,即从std::streambuf派生的类。

The state flags are present in std::ios but not in std::streambuf . 状态标志出现在std::ios但没有出现在std::streambuf

There are "stream buffer objects", and a "stream objects". 有“流缓冲对象”和“流对象”。 One stream buffer can be shared between multiple stream objects. 一个流缓冲区可以在多个流对象之间共享。 Each stream object has its own set of flags - so one stream may be "reached end of file", where another is not - or the flags for output in decimal or hex may be completely different for two output streams using the same buffer. 每个流对象都有其自己的标志集-因此,一个流可能会“到达文件末尾”,而另一流则不会-或对于使用同一缓冲区的两个输出流,以十进制或十六进制输出的标志可能完全不同。

[Of course, if you are using the same buffer for multiple streams, you will have to take care that you don't mess things up - and it's not a common thing to share the buffer over multiple streams, but it can be done!] [当然,如果您对多个流使用相同的缓冲区,则必须注意不要弄乱事情-在多个流上共享缓冲区不是一件普通的事,但是可以做到! ]

The iostate flags store things about output formatting: whether you want numbers printed in decimal or hex, capitals or lowercase, etc. Stream objects control formatting, so the flags are inside the stream object. iostate标志存储有关输出格式的信息:是否要以十进制或十六进制显示数字,大写或小写等。Stream对象控制格式,因此这些标志位于stream对象内部。

In iostreams, buffering is separate from formatting. 在iostream中,缓冲与格式化是分开的。 Linked to the iostream object is a stream buffer object, which controls sending and/or receiving characters from an underlying source. 流缓冲区对象链接到iostream对象,该对象控制从底层源发送和/或接收字符。 The buffer object has no such flags; 缓冲区对象没有这样的标志。 its only state variables deal with preparing (encoding) the characters and optionally storing (buffering) them to reduce the number of times the operating system is asked to perform I/O. 它唯一的状态变量处理字符的准备(编码)并有选择地存储(缓冲)字符,以减少要求操作系统执行I / O的次数。 (Or in the case of stringstream , the buffer provides the ultimate storage behind the stream.) (或者在使用stringstream的情况下,缓冲区将在流之后提供最终的存储。)

So a stream has state flags , but the stream buffer it uses doesn't . 因此,一个 具有状态标志 ,但它使用的流缓冲区没有

A stream buffer goes inside a stream. 流缓冲区进入流内部。

The buffer holds some amount of bytes that the stream is reading/writing before sending/recieving it to whatever the stream is talking to (file/stdin/tcpsocket/etc.). 缓冲区保存流正在读取/写入的一定数量的字节,然后再将其发送/接收到与之通信的任何内容(文件/ stdin / tcpsocket /等)。

Stream reference: http://www.cplusplus.com/reference/istream/iostream/ 流参考: http : //www.cplusplus.com/reference/istream/iostream/

Stream Buffer Reference: http://www.cplusplus.com/reference/streambuf/streambuf/ 流缓冲区参考: http : //www.cplusplus.com/reference/streambuf/streambuf/

By default a stream will usually create it's own stream buffer, but you can tell it to use one of your choosing in the constructor: http://www.cplusplus.com/reference/istream/iostream/iostream/ 默认情况下,流通常会创建自己的流缓冲区,但是您可以告诉它使用构造函数中的一种选择: http : //www.cplusplus.com/reference/istream/iostream/iostream/

Or you can get/set the buffer with the rdbuf method. 或者,您可以使用rdbuf方法获取/设置缓冲区。

The "stream buffer" is an object of class basic_streambuf. “流缓冲区”是basic_streambuf类的对象。 That class doesn't have state flags. 该类没有状态标志。 Every stream (basic_istream or basic_ostream) has a pointer to a basic_streambuf, but the flags are a property of the stream, not of the stream buffer. 每个流(basic_istream或basic_ostream)都有一个指向basic_streambuf的指针,但是标志是流的属性,而不是流缓冲区的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nicolai Josuttis在他的书中说,开放成员函数不会清除状态标志。 这不是我在VS2010中发现的。 这是MS问题吗? - Nicolai Josuttis says in his book that the open member function doesn't clear the state flags. That's not what I found in VS2010. Is this a MS issue? Nicolai Josuttis 的 c++17 演示文稿中用于初始化“客户类”的模板 - Template for initialization of the "customer class" from Nicolai Josuttis' c++17 presentation CppCon 2018,Nicolai Josuttis:为什么将这些解释为迭代器? - CppCon 2018, Nicolai Josuttis: Why are these interpreted as iterators? 我不明白这个 c++ 错误,但我需要帮助修复它 - I do not understand this c++ error but I need help fixing it 需要帮助以了解我在哪里出错 - Need help to understand where i am going wrong with strings 需要帮助,我不明白为什么以下代码无法编译 - Need help, I do not understand why following code is not getting compiled 我需要帮助以了解CLOCKS_PER_SEC - I need help to understand CLOCKS_PER_SEC 记忆围栏 - 需要帮助才能理解 - Memory Fences - Need help to understand 如何根据书的段落创建思维导图 - How to create a mind map from paragraph of a book Josuttis书中的PersonSortCriterion(第1版和第2版) - PersonSortCriterion in Josuttis book (1st edition vs 2nd edition)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM