this is same Question,但没有正确回答。
码
#include<iostream>
int main()
{
char ch='a';
std::cout<<ch;
}输出
a因此,在输出流中只有一个字符,这将导致缓冲区。所以缓冲区不是满的,它在屏幕上显示输出。意味着缓冲区被自动刷新。
因此,请给出一个例子,缓冲区不能自动刷新,我们必须使用机械手标志flush。
发布于 2021-06-19 07:48:24
您可以看到您的流是否通过在输出之间休眠来缓冲,例如:
#include <iostream>
#include <thread>
int main()
{
for (int i = 0; i < 50; i++)
{
char ch = 'a';
std::cout << ch << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}在某些平台上,您可能需要通过调用stdout来启用setvbuf上的缓冲
std::setvbuf(stdout, nullptr, _IOFBF, BUFSIZ);https://stackoverflow.com/questions/68044266
复制相似问题