仅手动冲洗 cout [英] Flush cout manually only

查看:30
本文介绍了仅手动冲洗 cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,将 std::endl 流式传输到 std::cout 会刷新它.我认为这种行为对大多数应用程序都有意义.

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

我的问题是我有一些使用多个 std::endl 的输出,因此刷新了输出.这对我的程序的性能来说真的很糟糕,而且还会导致很多图形故障,因为我经常跳来跳去.

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.

所以我的问题是我是否可以告诉 std::cout 等待下一次刷新,直到我明确调用 std::cout.flush() 或流 std::flushstd::cout.
如果这是可能的,我还想知道我如何才能扭转这种情况,因为这对我来说并不总是有意义.

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.

推荐答案

使用 std::cout <<'\n' 而不是 std::endl.这避免了每行之后的刷新.std::endl 将始终刷新,因为这是它的目的.没有选项可以禁用该行为.但是,根本不需要使用 std::endl.最终,您无法避免所有刷新,因为 std::cout 的缓冲区是有限的,因此最终,无论您是否使用 std::endl,输出都将被刷新或 '\n'.

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

如果你想增加标准输出的缓冲区大小,你可以尝试增加 cout 的缓冲区.

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

这篇关于仅手动冲洗 cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆