BrainStone
BrainStone

Reputation: 3155

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.

Upvotes: 4

Views: 2716

Answers (1)

b4hand
b4hand

Reputation: 9770

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.

Upvotes: 7

Related Questions