Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flush cout manually only

As far as I know streaming std::endl into std::cout will flush it. I get that that behavior makes sense for most applications.

My problem is that I have some output that uses multiple std::endl's and therefore flushes the output. This is really bad for the performance of my program and also causes a lot of graphical glitches since I am jumping around quite a lot.

So my question is if I can tell std::cout to wait with the next flush until I explicitly call std::cout.flush() or stream std::flush into std::cout.
If this is possible I'd also like to know how I then can reverse that since it does not always make sense for me.

like image 367
BrainStone Avatar asked Mar 17 '23 00:03

BrainStone


1 Answers

Use std::cout << '\n' instead of std::endl. This avoids the flush after every line. std::endl will always flush, since that is its purpose. There's no option to disable that behavior. However, there's no requirement to use std::endl at all. Ultimately, you can't avoid all flushing as the buffer for std::cout is finite, so eventually, the output will be flushed regardless if you use std::endl or '\n'.

If you want to increase the buffer size for standard output, you could try increase buffer for cout.

like image 127
b4hand Avatar answered Mar 25 '23 10:03

b4hand