问题描述
我正在尝试使用 c。但是,结果不会立即打印到控制台,而是会延迟(在两个周期或程序都完成之后)。
I am trying to print results in 2 nested for cycles using std::cout. However, the results are not printed to the console immediately, but with delay (after both for cycles or the program have been finished).
我认为这种行为不正常,在Windows下可以正常打印。该程序不使用线程。
I do not consider such behavior normal, under Windows printing works OK. The program does not use threads.
哪里出问题了? (Ubuntu 10.10 + NetBeans 6.9)。
Where could be the problem? (Ubuntu 10.10 + NetBeans 6.9).
推荐答案
std :: cout 是流,并且已缓冲。您可以通过几种方式刷新它:
std::cout is an stream, and it is buffered. You can flush it by several ways:
std::cout.flush(); std::cout << std::flush; std::cout << std::endl; // same as: std::cout << "\n" << std::flush`
约翰尼:
johny:
如果在之前刷新缓冲区>周期,该周期不会影响周期中的输出。您必须在循环中或循环后冲洗,以查看输出。
If you flush the buffer before the cycle, that does not affect the output in the cycle. You have to flush in or after the cycle, to see the output.
这篇关于Linux中std :: cout的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!