gpt4 book ai didi

c++ - 随机打印程序,尽管有 sleep() 函数,但一次打印多个输出?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:54:35 25 4
gpt4 key购买 nike

<分区>

所以,我有下面的程序。它的作用是打印随机数,同时在控制台上将它们格式化为宽度为 10。

现在,当我添加 sleep 功能时,我希望它每 10 毫秒(或更多)打印一个数字,但它所做的是,它每 100*10 毫秒打印 100 个输出。我想知道,为什么会这样?输出是否得到缓冲或什么?

#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>

int main()
{
srand(time(0));
for (int i = 1; i < 1000000; i++)
{
usleep(10*1000); // No matter the input, the same thing happens
cout << setw(10) << rand()%(90*i) + 10*i;
}
}

我在 windows 和 unix 上都试过了,结果完全一样。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com