c++ - 即使有时间延迟,cout 也不打印

标签 c++ time cout endl

<分区>

我希望 cout 打印“hello”,两秒后打印“world”。

int t = time( NULL );

std::cout << "hello";

while( time(NULL) < (t + 2) );

std::cout << " world";

但是,cout 会在两秒后将注释打印到屏幕上,然后程序会打印“hello world”。即使像(t + 9)这样增加时间延迟,也是一样的结果。我不熟悉这种 cout 行为。

但是如果我像这样在第一个 cout 处添加 std::endl:

std::cout << "hello" << std::endl;
...

我得到预期结果“hello”,两秒后得到“world”。

最佳答案

std::cout 通常是缓冲的,这意味着它可能不会立即输出,除非你强制它。尝试 std::flush在你的第一个输出之后:

std::cout << "hello " << std::flush;

关于c++ - 即使有时间延迟,cout 也不打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44180539/

相关文章:

python - Python中用户友好的时间格式?

c++ - 输出流中引用的奇怪行为

c++ - 为什么在 C++ 中,当我用 '+' 连接一个字符串时,它不连接而是删除字符串的第一个字符

c++ - 是否保证 std::unique_ptr 删除顺序?

C++模拟数字逻辑,实现友元二元运算符

c++ - 如何将第 3 个 C++ 方源代码编译为共享库

python - 在 DataFrame 中查找最近的时间

c# - 如何单步执行动态加载的 DLL 的源代码?

Javascript 时钟更新分钟帮助?

c++ - 制作控制台进度条? ( Windows )