Point 4 and Point 3
Starting from point 4, because everything else depends on it and point 3, because it is closely related.
When you clear a stream, you take all the data stored in the stream and write it to the underlying media represented by the stream.
When he blushed, he is done, perfect and ready for observation by the outside world (more or less). The operating system and hardware flow support may also delay recording, but you cannot do this). You cannot read it until it turns red. If he never blushed, you cannot read it.
The fact is that you do not want to write to IO very often, because everything that leaves the CPU takes an inhuman amount of time compared to what remains inside the CPU. Tens of thousands of times slower. Inside the processor, there are gigahertz and parallel buses that move data 32 or more bits at a time. Outside, you have megahertz often moving one bit at a time.
Take the file as a classic example. Not only does disk access work at processor speed, but if every byte goes directly to disk, then for each byte you may need
- find this byte counterpart on disk.
- loads a sector around this byte into memory. Therefore, instead of moving one byte, you can move several hundred or thousands. Typically 512 bytes or 4096 bytes.
- write a byte to a sector in memory
- write sector to memory back to disk
Brutal. Imagine doing this several hundred or several thousand times to write a single line. But what if you only wrote a line when it got too big to hold, or did you finish? What if you write in sectors, not in bytes? Then you could
- find this byte in the analog sector on disk.
- write saved sector to disk
One operation for potentially thousands of bytes per shot.
Point 2
Point 2 goes back to point four / three, you cannot read what you are not doing. If you want to see a specific output on the screen, and you want to see it now, you will flash. If you want to receive a debugging message before the program crashes, and most likely will end without receiving the last last absolutely necessary messages on the screen, you will flash. The story is full of programmers who are looking for the wrong place to make a mistake because they have not received those last few messages about common errors.
You trade program speed for relative confidence that you will see an important message when you need to see it.
Point 1
Point 1 refers to point 2. std::endl is both the end of the line and the instruction to reset the stream. You use it sparingly and only when you need both the end of the line and the flash. If you don't need a flash, just send and end the line: '\n' .
Take Pete Becker's advice and use std::cerr for errors and debugging where possible. This is what it was built for. He acts on brute force and ignorance. This is painful. It is slow. And it almost always works.