在 C++ 中暂停 For 循环 1 秒

Blu*_*erg 2 c++ sleep

我有一个循环,但速度很快。我需要一些简单易用的东西,在每个循环中暂停 1 秒。

for(int i=0;i<=500;i++){
   cout << "Hello number : " << i;
   //i need here something like a pause for 1 sec
}

Run Code Online (Sandbox Code Playgroud)

Den*_*met 6

std::this_thread::sleep_for正是您正在寻找的。

for(int i=0;i<=500;i++){
    cout << "Hello number : " << i;
    std::this_thread::sleep_for(1s);
}
Run Code Online (Sandbox Code Playgroud)

要像这样使用它,您需要包含<chrono><thread>然后添加using namespace std::chrono_literals;. 它还需要c++11启用。