时间流c++学习笔记(1)

#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;

     
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值