c语言equal,C ++中的ratio_equal()示例

在本文中,我们将讨论C ++ STL中ratio_equal()函数的工作,语法和示例。

什么是ratio_equal模板?

ratio_equal模板内置在C ++ STL中,该模板在头文件中定义。ratio_equal用于比较两个比率。该模板接受两个参数,并检查给定的比率是否相等。就像我们有两个比率1/2/3/6一样,当我们简化它们时,它们是相等的,但是数字不相等,因此C ++有一个内置模板来检查两个比率是否相等,如果相等,则返回true假。

因此,当我们要检查两个比率的相等性时,可以使用提供的模板来简化编码,而不必使用C ++编写整个逻辑。

语法template  ratio_equal;

参数

模板接受以下参数-ratio1,ratio2-这是我们要检查的两个比率是否相等。

返回值

当两个比率相等时,该函数返回true,否则该函数返回false。

输入项typedef ratio<3, 6> ratio1;

typedef ratio<1, 2> ratio2;

ratio_equal::value;

输出结果true

输入项typedef ratio<3, 9> ratio1;

typedef ratio<1, 2>ratio2;

ratio_equal::value;

输出结果false

示例#include 

#include 

using namespace std;

int main(){

typedef ratio<2, 5> R_1;

typedef ratio<10, 25> R_2;

//检查比率是否相等

if (ratio_equal::value)

cout<

else

cout<

return 0;

}

输出结果

如果我们运行上面的代码,它将生成以下输出-Ratio 1 and Ratio 2 are equal

示例#include 

#include 

using namespace std;

int main(){

typedef ratio<2, 5> R_1;

typedef ratio<1, 3> R_2;

//检查比率是否相等

if (ratio_equal::value)

cout<

else

cout<

return 0;

}

输出结果

如果我们运行上面的代码,它将生成以下输出-Ratio 1 and Ratio 2 aren’t equal

示例Code-3:

//如果我们尝试在分母中输入0,则输出为

#include 

#include 

using namespace std;

int main(){

typedef ratio<2, 5> R_1;

typedef ratio<1, 0> R_2;

//检查比率是否相等

if (ratio_equal::value)

cout<

else

cout<

return 0;

}

输出结果

如果我们运行上面的代码,它将生成以下输出-/usr/include/c++/6/ratio:265:7: error: static assertion failed: denominator cannot be zero

static_assert(_Den != 0, "denominator cannot be zero");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值