【Cpp 基础】主动刷新 cout 缓存区——使用<<flush

cout 有缓存区,因此在一些情况下,并不会主动显示出来。
在这种情况下,可使用额外的 “刷新” 功能(<<flush)来确保根据我们的要求显示输出。

// C++程序演示flush函数的使用
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
	for (int i = 1; i <= 5; ++i)
	{
		cout << i << " " << flush;
		this_thread::sleep_for(chrono::seconds(1));
	}
return 0;
}
posted @ 2023-11-08 16:25  FBshark  阅读(90)  评论(0编辑  收藏  举报