c ++ force std :: cout flush(print to screen) [英] c++ force std::cout flush (print to screen)

查看:306
本文介绍了c ++ force std :: cout flush(print to screen)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

std::cout << "Beginning computations..."; // output 1
computations();
std::cout << " done!\n";                  // output 2

然而,问题是输出#1和输出#2虚拟)。也就是说,在 calculateations()返回之前,常常输出#1不会被打印到屏幕上。由于输出#1的整个目的是指示某事在后台进行(因此鼓励用户的耐心),这个问题不好。

The problem, however, is that often output #1 and output #2 appear (virtually) simultaneously. That is, often output #1 does not get printed to the screen until after computations() returns. Since the entire purpose of output #1 is to indicate that something is going on in the background (and thus to encourage patience from the user), this problem is not good.

是否有任何方法强制 std :: cout 缓冲区在 computeations()调用之前打印?或者,是否有其他方法(使用 std :: cout 之外的其他方法)打印到标准输出,这将解决这个问题?

Is there any way to force the std::cout buffer to get printed before the computations() call? Alternatively, is there some other way (using something other than std::cout) to print to standard out that would fix this problem?

推荐答案

只需插入 std :: flush

std::cout << "Beginning computations..." << std::flush;

另请注意,插入 std :: endl 也会在写一个换行符后清除。

Also note that inserting std::endl will also flush after writing a newline.

这篇关于c ++ force std :: cout flush(print to screen)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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