I am still confused about what is in the buffer that should be flushed and is it visible in the results? |
every output stream object holds a buffer (an array of characters), somewhere in memory.
Writing a character to a stream means copying the character to the buffer and incrementing the next pointer (which is a data member of every stream object), that's a fast, simple operation.
When the buffer is full or when a flush is requested, the contents of the buffer are sent to the actual output device (screen, file, pipe, compressed file, network connection, in-memory data structure, whatever the author of the stream have set up). That usually means asking the OS to do some work, and that is relatively slow.
If you are working with std::cout, you don't need to flush it yourself usually because every input from std::cin flushes it for you, so that interactive programs work as expected: you will always see the output before any input is requested. Also calling exit(), ending the main function, and sending output to std::cerr flushes std::cout for you as well. Also, on most systems with default settings, sending just the \n to std::cout flushes it (although in a slightly different sense) without any effort on your part.
You may need a flush if your program has to wait for something other than input, e.g. you're doing a long computation and printing progress so far, or calling system() to wait for some external program.