在C/C++中精确延迟若干秒
#include <iostream>
#include <ctime>
//#include <time.h> C语言使用的头文件
using namespace std;
/**
* @brief 延迟若干秒
*
* @param n :延迟的秒数
*/
void Delay_s(float n)
{
clock_t delay = CLOCKS_PER_SEC * n; //CLOCKS_PER_SEC为一秒的系统节拍数
clock_t start = clock(); //获取当前的系统时钟值
while(clock()-start < delay);
}
int main()
{
Delay_s(0.9);
cout << "hello world!" << endl;
return 0;
}