#include <iostream>
#include<chrono>
using namespace std;
/*
/*
* duration类:表示的是一段时间间隔,用来记录时间的长度,可以表示几秒几分几时
* ratio<1>一秒
* ratio<60>一分
* ratio<60*60>一时 而在此,我们可以知道这不过是一种缺省的方式使用,其实正规应该是<_,_>
* ratio<60*60*24>一天
* ratio<1,100>毫秒 1/100,100是分母、1是分子,缺省使用时自动定为1.
* ratio<1,1000000>微秒
*
*/
int main()
{
chrono::duration<int, ratio<1>> sec;
//上面的等效于下边的
chrono::seconds s(1);
//chrono::naoseconds
//chrono::seconds
//chrono::minutes
//chrono::hours 以后就这么用把
std::chrono::nanoseconds ns(1);
}
转定义后看c++官方已经封装好了常用时间库,可以直接调用
#include <iostream>
#include<chrono>
#include<thread>
using namespace std;
//this_thread::slepp_for() 在this_thread命名空间的延迟函数
void testDuration()
{
cout << "开始" << endl;
this_thread::sleep_for(chrono::seconds(1));//延迟的参数就是chrono空间的时间函数
cout << "结束" << endl;
this_thread::sleep_for(chrono::milliseconds(2000));
cout << "两秒结束" << endl;
//通过时间间隔的类构建了一个时间间隔
//而我们实际上常用用的是重载文本类型的操作
this_thread::sleep_for(1s);//实际上,这是个this_thread命名空间内的线程处理函数
cout << endl;
this_thread::sleep_for(1h);//工作中也常常用这个来休眠程序来达到某种目的
cout << endl;
//关于时间间隔的时间运算
chrono::seconds test(100);//构建个100s出来
cout << test.count() << endl;//打印出来显示
//在做时间换算的时候,就是加减之类的可以直接用auto偷一下懒
chrono::hours h(1);//构建1小时;
auto result = h - test;//可以直接相减了这样,当然,结果是秒数
cout << result.count() << endl;
chrono::duration<double, ratio<3, 5>> time;// double类型的 3/5 秒=0.6秒
cout << time.count() << endl;
}
int main()
{
testDuration();
return 0;
}