我有一个循环,但速度很快。我需要一些简单易用的东西,在每个循环中暂停 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)
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启用。