简体   繁体   English

c ++中cout缓冲区和endl的结构

[英]structure of cout buffer and endl in c++

I am new to C++ learning from C++ Primer book. 我是新的C++的学习C++ Primer书。 In the first chapter, writer talks about the buffer of the iostream , cout , and endl . 在第一章中,作者讨论了iostreamcoutendl的缓冲区。 I could not understand well. 我听不懂。 I have two example codes here. 我这里有两个示例代码。

#include<iostream>
int v1;
int main()
{

    std::cout<<"First Cout!";
    std::cin>>v1;
    std::cout<<"Second Cout!"<<std::endl;
    std::cout<<"Third Cout!\n";

    return 0;
}

I want to know the state of the cout buffer after execution of each line. 我想知道每一行执行后cout缓冲区的状态。

The stream contains an in-memory buffer where data is written before being flushed to the final destination (in this case, the output console), since flushing can be an expensive operation. 该流包含一个内存缓冲区,在将数据刷新到最终目的地(在本例中为输出控制台)之前,先写入数据,因为刷新可能是一项昂贵的操作。

The buffer might be flushed automatically in some circumstances: when the stream is closed, or if there's a lot of buffered data, or if the stream is configured to flush after each line, as std::cerr is. 在某些情况下,缓冲区可能会自动刷新:关闭流时,或者如果有很多缓冲的数据,或者流配置为在每行之后刷新,如std::cerr

Sometimes you need to flush manually, for example to make sure the user sees something you've written to std::cout . 有时您需要手动刷新,例如,确保用户看到您已写入std::cout That can be done in two ways: 这可以通过两种方式完成:

  • calling the flush() member function on the stream; 在流上调用flush()成员函数;
  • streaming the std::flush manipulator into the stream. std::flush操纵器流式传输到流中。

The std::endl manipulator does two things: std::endl操纵器执行两项操作:

  • inserts a new-line character (or character sequence), equivalent to << '\\n' ; 插入换行符(或字符序列),等效于<< '\\n' ; then 然后
  • flushes the stream, equivalent to << std::flush , (which is in turn calls the stream's flush() member function). 刷新流,等效于<< std::flush (依次调用流的flush()成员函数)。

This is useful for writing short messages to the console; 这对于将短消息写入控制台非常有用; but should be used carefully, as it can have a considerable performance impact when producing large amounts of output. 但应谨慎使用,因为它在产生大量输出时可能会对性能产生重大影响。

A further complication is that one stream can be tied to another, so that one is flushed before another is accessed. 更复杂的是,一个流可以另一流绑定 ,因此在访问另一个流之前先对其进行刷新。 In this case, cout is tied to cin , which is why you see the first output before reading from cin even though there is no explicit flush. 在这种情况下, coutcin绑定在一起,这就是为什么即使没有显式刷新也要在从cin读取之前看到第一个输出的原因。

Finally, all the standard streams, including cout , are flushed automatically when the program ends (specifically, when the global instance of std::ios_base::Init is destroyed; but that's a detail you shouldn't need to know about.) 最后,所有标准流(包括cout在程序结束时都会自动刷新(特别是在std::ios_base::Init的全局实例被破坏时;但这是您无需了解的详细信息。)

After first line, the output is in the buffer, so you will not see it in the terminal. 第一行之后,输出在缓冲区中,因此您不会在终端中看到它。

After the second line, the endl causes the buffer to be flushed so you will now see line 1 and 2 in the terminal output. 在第二行之后, endl导致缓冲区被刷新,因此您现在将在终端输出中看到行1和2。

After the third line, output is in the buffer and you will not see it in the terminal until the program exits. 第三行之后,输出在缓冲区中,直到程序退出,您才会在终端中看到它。

Edit: 编辑:

When you place a cin between line 1 and 2, it causes cout to be flushed. 在第1行和第2行之间放置cin时,会导致cout被冲洗。 See: std::cin 请参阅: std :: cin

cin is tied to the standard output stream cout (see ios::tie), which indicates that cout's buffer is flushed (see ostream::flush) before each i/o operation performed on cin. cin与标准输出流cout绑定(请参阅ios :: tie),这表示在cin上执行每个I / O操作之前,已清空cout的缓冲区(请参见ostream :: flush)。

After 1st line, cout contains just the given string with no newline char. 第一行之后,cout仅包含给定的字符串,没有换行符char。 After 2nd line, cout additionally contains a line ending and the text appears at the console window. 在第二行之后,cout另外包含一行结尾,并且文本出现在控制台窗口中。 After 3rd line, cout contains another string including line ending which not necessarily appears at the console window. 在第三行之后,cout包含另一个包含行尾的字符串,该字符串不一定出现在控制台窗口中。 After exiting main, the text of the 3rd line is appears. 退出main后,出现第三行的文本。

std::endl just adds EOL (end of line) symbol to output. std :: endl只是在输出中添加EOL(行尾)符号。 Usually its \\n, but it can vary from one OS to another. 通常是\\ n,但是从一个操作系统到另一个操作系统可能有所不同。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM