https://www.cnblogs.com/bianchengnan/p/9478844.html
概览
std::ratio
定义在<ratio>
文件中,提供了编译期的比例计算功能。为std::chrono::duration
提供基础服务。
类定义
std::ratio是一个模板类,关键代码摘录如下(格式有调整):
template<intmax_t _Nx, intmax_t _Dx = 1>
struct ratio
{
static_assert(_Dx != 0, "zero denominator");
static_assert(-INTMAX_MAX <= _Nx, "numerator too negative");
static_assert(-INTMAX_MAX <= _Dx, "denominator too negative");
static constexpr intmax_t num = _Sign_of<_Nx>::value
* _Sign_of<_Dx>::value * _Abs<_Nx>::value / _Gcd<_Nx, _Dx>::value;
static constexpr intmax_t den = _Abs<_Dx>::value / _Gcd<_Nx, _Dx>::value;
typedef ratio<num, den> type;
};
第一个参数_Nx
代表了分子,第二个参数 _Dx
代表了分母。
num是计算后的分子,den是计算后的分母。在duration转换的时候会用到这两个值。
注:这里的计算是指约分,可以看到传入的分子和分母都除以了最大公约数。
num是numerator的缩写,表示分子。
den是denominator的缩写,表示分母。
预定义ratio
为了方便代码的书写,标准库提供了如下定义:
Type Definition
yocto std::ratio<1, 1000000000000000000000000>, if std::intmax_t can represent the denominator
zepto std::ratio<1, 1000000000000000000000>, if std::intmax_t can represent the denominator
atto std::ratio<1, 1000000000000000000>
femto std::ratio<1, 1000000000000000>
pico std::ratio<1, 1000000000000>
nano std::ratio<1, 1000000000>
micro std::ratio<1, 1000000>
milli std::ratio<1, 1000>
centi std::ratio<1, 100>
deci std::ratio<1, 10>
deca std::ratio<10, 1>
hecto std::ratio<100, 1>
kilo std::ratio<1000, 1>
mega std::ratio<1000000, 1>
giga std::ratio<1000000000, 1>
tera std::ratio<1000000000000, 1>
peta std::ratio<1000000000000000, 1>
exa std::ratio<1000000000000000000, 1>
zetta std::ratio<1000000000000000000000, 1>, if std::intmax_t can represent the numerator
yotta std::ratio<1000000000000000000000000, 1>, if std::intmax_t can represent the numerator
应用
如果想表示1/5,那么可以这样写std::ratio<1, 5>。
回想我们在学习std::chrono::duration中遇到到的milli的定义:
typedef ratio<1, 1000> milli;,表示一千分之一,因为约定了基本计算单位是秒,所以milli表示一千分之一秒。
示例代码
#include <iostream>
#include <chrono>
typedef std::chrono::duration<float, std::ratio<3, 1> > three_seconds;
typedef std::chrono::duration<float, std::ratio<1, 10> > one_tenth_seconds;
int main()
{
three_seconds s = std::chrono::duration_cast<three_seconds>(one_tenth_seconds(3));
std::cout << "3 [1/10 seconds] equal to " << s.count() << " [3 seconds]\n";
std::cin.get();
}
参考资料
vs源码
cppreference