1 除数取整
1.C++向上取整
eg:7/3向上取整
int x = 7;
int y = 3;
int ans = (x+y-1)/3; //3
int ans = ceil(x/y);
2.向下取整
int x = 7;
int y = 3;
int ans = x/y;
int ans = floor(x/y);
3.四舍五入
int x = 7;
int y = 3;
int ans = (x+y/2)/y;
int ans = round(x/y);
2C++延时等待
添加链接描述
1.Sleep
#inlude<windows.h>
//单位是毫秒
Sleep(5*1000) //5s延时
2.Delay
//利用时钟延时
#include<ctime>
void Delay(int time){
clock_t now = clock();
while (clock()-now<time);
}
Delay(5*100)
3时间计算
3.1 std::chrono
- 是C++11boost库。当需要接受时间信息时使用
#include<iostream>
#include<chrono>
#include<thread>
//重载ns/us/ms/s/min/h
//做计时器
//给一个时间值,时间值之间的内部转换
void sleep1severytime() {
//1.直接使用
std::this_thread::sleep_for(std::chrono::seconds(1));
//2.定义一个时间值再使用
std::chrono::seconds sec1(1);
std::this_thread::sleep_for(sec1);
std::cout << sec1.count() << std::endl;
//3.自定义时间,std::chrono::duration默认为s,形参1为计算类型,形参二为哪两个值相除,如<1,2>为0.5,<3600,1>为1h
std::chrono::duration<double, std::ratio<1, 2>> sec_0_5s;
auto sec1s = sec_0_5s * 2;
std::this_thread::sleep_for(sec1s);
//4.需要声明函数空间域
using namespace std::chrono;
std::this_thread::sleep_for(1s);
//时间差分结果
//180-3 = 177
std::chrono::minutes min3(3);
std::chrono::seconds sec3(3);
//auto secdiff = min3 - sec3; 等价下面
std::chrono::seconds secdiff = min3 - sec3;
std::cout << secdiff.count() << std::endl;
//180
std::chrono::seconds min2sec = min3;
std::cout << min2sec.count() << std::endl;
}
int main() {
std::thread T1 = std::thread(sleep1severytime);
T1.join();
}