C++11中头文件ratio的使用
include<ratio>是在C++11中引入的,在此文件中有一些模板类。
模板类std::ratio及相关的模板类(如std::ratio_add)提供编译时有理数算术支持。此模板的每个实例化都准确表示任一有限有理数。它们都是用来表示比例关系的模板类。
std::ratio的声明:
template<intmax_t N, intmax_t D = 1> class ratio;
其中N表示分子,D表示分母;intmax_t表示最大的有符号整数类型,N和D的绝对值都应该在intmax_t可表示的范围内,D不能是0。
std::ratio类一般不通过它的对象来表示,而是这个类型本身来表示的,但也可以通过它的对象来表示。std::ratio有两个成员常量:num表示的是分子,den表示的是分母。这里的num和den已经经过化简,因此输出值可能和定义时传入的不同,如typedef std::ratio<100, 10> ratio1, 输出值ratio1::num为10,ratio1::den为1.
在include<ratio>中,除std::ratio外,还有编译时有理数算术:std::ratio_add、std::ratio_subtract、std::ratio_multiply、std::ratio_divide,它们和std::ratio一样,也都有两个成员常量:num和den。编译时有理数比较:std::ratio_equal、std::ratio_not_equal、std::ratio_less、std::ratio_less_equal、std::ratio_greater、std::ratio_greater_equal,它们都有value成员常量。它们的计算公式如下:
template <typename R1, typename R2>
using ratio_add = ratio < R1::num*R2::den+R2::num*R1::den, R1::den*R2::den >template <typename R1, typename R2>
using ratio_subtract = std::ratio < R1::num*R2::den-R2::num*R1::den, R1::den*R2::den >template <typename R1, typename R2>
using ratio_multiply = std::ratio < R1::num * R2::num, R1::den * R2::den >;template <typename R1, typename R2>
using ratio_divide = ratio < R1::num * R2::den, R2::num * R1::den >;template <class R1, class R2>
struct ratio_equal : integral_constant<bool, R1::num==R2::num && R1::den==R2::den> {}template <class R1, class R2>
struct ratio_less : integral_constant < bool, R1::num*R2::den < R2::num*R1::den > {};template <class R1, class R2>
struct ratio_greater : integral_constant < bool, ratio_less<R2,R1>::value > {};template <class R1, class R2>
struct ratio_greater_equal : integral_constant < bool, !ratio_less<R1,R2>::value > {};template <class R1, class R2>
struct ratio_less_equal : integral_constant < bool, !ratio_less<R2,R1>::value > {}template <class R1, class R2>
struct ratio_equal : integral_constant < bool, !ratio_equal<R1,R2>::value > {};
下标剪切于: cplusplus
下面是从其他文章中copy的<ratio>测试代码,详细内容介绍可以参考对应的reference:
#include "ratio.hpp"
#include <iostream>
#include <ratio>//
// reference: http://www.cplusplus.com/reference/ratio/
int test_ratio_1()
{typedef std::ratio<1, 3> one_third;typedef std::ratio<2, 4> two_fourths;typedef std::ratio<2, 3> two_thirds;typedef std::ratio<1, 2> one_half;std::cout << "one_third= " << one_third::num << "/" << one_third::den << std::endl;std::cout << "two_fourths= " << two_fourths::num << "/" << two_fourths::den << std::endl; // Note: 1/2std::cout << "two_thirds= " << two_thirds::num << "/" << two_thirds::den << std::endl;std::cout << "one_half= " << one_half::num << "/" << one_half::den << std::endl;std::cout << std::endl;{ // std::ratiotypedef std::ratio_add<one_third, two_fourths> sum;std::cout << "sum= " << sum::num << "/" << sum::den;std::cout << " (which is: " << (double(sum::num) / sum::den) << ")" << std::endl;std::cout << "1 kilogram has " << (std::kilo::num / std::kilo::den) << " grams";std::cout << std::endl;
}{ // std::ratio_addtypedef std::ratio_add<one_half, two_thirds> sum;std::cout << "sum = " << sum::num << "/" << sum::den;std::cout << " (which is: " << (double(sum::num) / sum::den) << ")" << std::endl;
}{ // std::ratio_subtracttypedef std::ratio_subtract<two_thirds, one_half> diff;std::cout << "diff = " << diff::num << "/" << diff::den;std::cout << " (which is: " << (double(diff::num) / diff::den) << ")" << std::endl;
}{ // std::ratio_multiplytypedef std::ratio_multiply<one_half, one_third> result;std::cout << "result = " << result::num << "/" << result::den;std::cout << " (which is: " << (double(result::num) / result::den) << ")" << std::endl;
}{ // std::ratio_dividetypedef std::ratio_divide<one_half, one_third> result;std::cout << "result = " << result::num << "/" << result::den;std::cout << " (which is: " << (double(result::num) / result::den) << ")" << std::endl;std::cout << std::endl;
}{ // std::ratio_equalstd::cout << "1/2 == 2/4 ? " << std::boolalpha;std::cout << std::ratio_equal<one_half, two_fourths>::value << std::endl;
}{ // std::ratio_greaterstd::cout << "1/3 > 1/2 ? " << std::boolalpha;std::cout << std::ratio_greater<one_third, one_half>::value << std::endl;
}{ // std::ratio_greater_equalstd::cout << "1/3 >= 1/2 ? " << std::boolalpha;std::cout << std::ratio_greater_equal<one_third, one_half>::value << std::endl;
}{ // std::ratio_lessstd::cout << "1/3 < 1/2 ? " << std::boolalpha;std::cout << std::ratio_less<one_third, one_half>::value << std::endl;
}{ // std::ratio_less_equalstd::cout << "1/3 <= 1/2 ? " << std::boolalpha;std::cout << std::ratio_less_equal<one_third, one_half>::value << std::endl;
}{ // std::ratio_not_equalstd::cout << "1/2 != 2/4 ? " << std::boolalpha;std::cout << std::ratio_not_equal<one_half, two_fourths>::value << std::endl;
}std::cout << std::endl;return 0;
}/
// reference: https://stackoverflow.com/questions/25005205/why-can-i-have-a-ratio-object-in-c
int test_ratio_2()
{// If you don't use a typedef you're creating an instance of std::ratio<1, 3> named one_third,// which is not suitable for passing as a type argument.In that case you'll need to use decltype// to get to the appropriate type that can be passed to ratio_addstd::ratio<1, 3> one_third;std::ratio<2, 4> two_fourths;std::ratio_add<decltype(one_third), decltype(two_fourths)> sum;std::cout << decltype(sum)::den << std::endl;return 0;
}
GitHub:https://github.com/fengbingchun/Messy_Test
C++11中头文件ratio的使用相关推荐
- C++11中头文件chrono的使用
在C++11中,<chrono>是标准模板库中与时间有关的头文件.该头文件中所有函数与类模板均定义在std::chrono命名空间中. std::chrono是在C++11中引入的,是一个 ...
- C++11中头文件type_traits介绍
C++11中的头文件type_traits定义了一系列模板类,在编译期获得某一参数.某一变量.某一个类等等类型信息,主要做静态检查. 此头文件包含三部分: (1).Helper类:帮助创建编译时常量的 ...
- C++11中头文件atomic的使用
原子库为细粒度的原子操作提供组件,允许无锁并发编程.涉及同一对象的每个原子操作,相对于任何其他原子操作是不可分的.原子对象不具有数据竞争(data race).原子类型对象的主要特点就是从不同线程访问 ...
- C++11中头文件thread的使用
C++11中加入了<thread>头文件,此头文件主要声明了std::thread线程类.C++11的标准类std::thread对线程进行了封装.std::thread代表了一个线程对象 ...
- C++/C++11中头文件sstream介绍
C++使用标准库类来处理面向流的输入和输出:(1).iostream处理控制台IO:(2).fstream处理命名文件IO:(3).stringstream完成内存string的IO. 类fstrea ...
- C++/C++11中头文件functional的使用
<functional>是C++标准库中的一个头文件,定义了C++标准中多个用于表示函数对象(function object)的类模板,包括算法操作.比较操作.逻辑操作:以及用于绑定函数对 ...
- C++/C++11中头文件algorithm的使用
<algorithm>是C++标准程序库中的一个头文件,定义了C++ STL标准中的基础性的算法(均为函数模板).<algorithm>定义了设计用于元素范围的函数集合.任何对 ...
- C++/C++11中头文件numeric的使用
<numeric>是C++标准程序库中的一个头文件,定义了C++ STL标准中的基础性的数值算法(均为函数模板): (1).accumulate: 以init为初值,对迭代器给出的值序列做 ...
- C++/C++11中头文件iterator的使用
<iterator>是C++标准程序库中的一个头文件,定义了C++ STL标准中的一些迭代器模板类,这些类都是以std::iterator为基类派生出来的.迭代器提供对集合(容器)元素的操 ...
最新文章
- 欧盟AI道德准则草案出炉
- thinkphp视频截图_thinkphp开发的搞笑视频网站
- hdu 2553 N皇后问题(深度递归搜索)
- 中科院分词系统(NLPIR)JAVA简易教程
- 最小完美哈希函数简介
- 美国只有两样东西比中国贵
- JS-鼠标-键盘-文档-表单事件
- css奇数、偶数样式
- queryrunner带参数的构造函数和不带参数的构造函数有什么区别_再谈构造函数、原型、原型链之间的关系...
- aspose.cell for java 去水印_【Java编程基本功】(十)输出杨辉三角,输出*号,数组移动...
- BIM平台 http://gzcd.bim001.cn
- oracle instr函数 判断字段中是否有换行符
- 造假家族覆灭记:警方联合阿里斩断涉案三千万假耐克产销链
- 蓝桥杯算法题--跑步锻炼(java实现)
- 树莓派(Raspberry Pi)安装五笔输入法,五笔拼音混合输入法
- (杂谈二) 看看别人是怎么写程序的(程序员的自我修养)
- Random随机生成几位数字和UUID随机生成几位字母加数字组合
- Iconfont 开源库 Iconic 使用 方便你使用svg图片。随意更改你的色调。
- python有什么好玩的书_史上最全的Python书排行榜|你想知道的都在这里
- 两种降压型DC-DC变换器的工作原理