C++时间管理,随机数

1. 时间管理:

时间段:表示时间间隔,秒,微妙啊等等。。。

时钟: system_clock:

steady_clock:

high_resolution_clock:

公用的函数:

static time_point now() ;获取当地时间点

static time64 to_time_t(const time_point x);

time_point from_time_t(time_t x);

时间转换

duration_cast 不属于duration类

浮点时长和整数时长之间可以直接隐式转换

#include <chrono>//事件处理头文件
#include <iostream>
#include <thread>
#include <iomanip>
using namespace std;
using namespace chrono;//使用时间管理的空间命名
int main()
{
	/**********时间段**********/
	duration<long long,ratio<60>> dur;
	hours h(1);//构造1小时
	minutes m(60);
	seconds s(3600);//构造3600秒
	microseconds ms(1);
	//等等等
	seconds result = h - s;
	cout << result.count() << endl;
	this_thread::sleep_for(seconds(10));//延时10秒
	this_thread::sleep_for(10s);//延时10秒
	/*******时钟类*******/
	system_clock::time_point result = system_clock::now();//获取当前时间
	time_t m_tm = system_clock::to_time_t(result);//转化为可以显示的time_t
	cout << ctime(&m_tm) << endl;//小黑显示 Wed Jan  5 20:38:38 2022
	tm* p = localtime(&m_tm);
	cout << "格式化时间" << put_time(p, "%F %T") << endl;//小黑显示 格式化时间2022 - 01 - 05 20:41 : 28
	//更多时间显示方式见官方文档
	cout << result.time_since_epoch().count() << endl;//打印时间戳
	//就是从1970到现在有多少秒了  小黑显示16413868269812447
	high_resolution_clock::time_point result = high_resolution_clock::now();//高精度时钟获取当前时间
	return 0;
}

2. 随机数:

种子序列:seed_seq类

       size()检测种子个数

       generate()函数

       param()函数

引擎适配器:

shuffle_order_engine:乱序随机数引擎适配器

independent_bits_engine:独立位随机数引擎适配器

discard_block_engine:丢弃块随机数引擎适配器

default_random_engine:默认随机数引擎适配器

#include <iostream>
#include <random>
#include <functional>
#include <array>
#include <chrono>
using namespace std;
using namespace chrono;
int main()
{
	seed_seq seed = { 1,2,3,4,5,6 };
	cout << seed.size() << endl;
	array<int, 7> data;
	seed.param(data.begin());


	//生成随机数
	default_random_engine e;
	e.seed((size_t)high_resolution_clock::now().time_since_epoch().count());
	for (int i = 0; i < 3; i++)
	{
		cout << e()%100 << endl;//输出100内随机数
	}
	cout << e.max() << e.min();//随机数的最大值最小值
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值