简体   繁体   中英

structure of cout buffer and endl in c++

I am new to C++ learning from C++ Primer book. In the first chapter, writer talks about the buffer of the iostream , cout , and endl . 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.

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.

Sometimes you need to flush manually, for example to make sure the user sees something you've written to std::cout . That can be done in two ways:

  • calling the flush() member function on the stream;
  • streaming the std::flush manipulator into the stream.

The std::endl manipulator does two things:

  • inserts a new-line character (or character sequence), equivalent to << '\\n' ; then
  • flushes the stream, equivalent to << std::flush , (which is in turn calls the stream's flush() member function).

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.

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.)

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.

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. See: 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.

After 1st line, cout contains just the given string with no newline char. After 2nd line, cout additionally contains a line ending and the text appears at the console window. After 3rd line, cout contains another string including line ending which not necessarily appears at the console window. After exiting main, the text of the 3rd line is appears.

std::endl just adds EOL (end of line) symbol to output. Usually its \\n, but it can vary from one OS to another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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