gpt4 book ai didi

c++ - 如何在 C++ 代码中添加延迟。

转载 作者:太空宇宙 更新时间:2023-11-04 15:32:38 25 4
gpt4 key购买 nike

我想添加一个延迟,以便运行一条线路,然后在短暂延迟后运行第二条线路。我是 C++ 的新手,所以我不确定我会怎么做。所以理想情况下,在下面的代码中它会打印“Loading...”并等待至少 1-2 秒,然后再次打印“Loading...”。目前,它会立即打印而不是等待。

cout << "Loading..." << endl;
// The delay would be between these two lines.
cout << "Loading..." << endl;

最佳答案

在 C++ 11 中,您可以使用此线程和 crono 来执行此操作:

#include <chrono>
#include <thread>
...
using namespace std::chrono_literals;
...
std::this_thread::sleep_for(2s);

关于c++ - 如何在 C++ 代码中添加延迟。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46230710/

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