为什么sleep()命令在我用c++编写时不起作用

  • 本文关键字:c++ 不起作用 sleep 命令 c++
  • 更新时间 :
  • 英文 :


每次我使用sleep((时,它只是说它不被识别:

main.cpp: In function 'int main()':
main.cpp:16.5: error: 'sleep' was not declared in this scope
16 |    sleep(2);
|    ^~~~~

我一直在查,但似乎什么都不起作用

#include < iostream >
using namespace std;
int main()
{
cout<<"Hello World";
sleep(2);
cout<<"hello world";
return 0;
}
#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
using std::chrono::system_clock;
auto now_str()
{
auto now = system_clock::to_time_t(system_clock::now());
return std::ctime(&now);
}
int main()
{
std::cout << "Start: " << now_str() << std::flush;
std::this_thread::sleep_for(2s);
std::this_thread::sleep_for(200ms);
std::cout << " Stop: " << now_str() << std::flush;
return 0;
}

现场演示

  • 标准::chrono_literals
  • std::this_thread::sleep_for
  • system_clock::to_time_t
  • std::ctime

最新更新