C++ 中的 Ratio_greater_equal() 函数


给定任务是展示 C++ 中 ratio_greater_equal() 函数的工作原理。

给定的函数 Ratio_greater_equal 检查 ratio1 的值是否大于或等于 ratio2。它返回一个布尔常量“value”,如果 ratio1 大于或等于 ratio2 则返回 true,否则返回 false。

语法

Template <ratio1, ratio2> ratio_greater_equal

参数

此函数接受两个模板参数,一个是 ratio1,另一个是 ratio2,它们将被比较。

此函数的解释

在此函数中,如果 ratio1 的值大于或等于 ratio2 的值,则此函数将返回布尔值 true,即整数数字 1,否则将返回 false,即整数数字 0。

typedef 的解释

Typedef 用于为数据类型赋予一个新名称,在此程序中,我们使用 typedef 来声明比率。Typedef 创建别名,可在任何地方代替类型名称使用,它可以在同一行声明一个或多个标识符,并且还可以用于声明数组和函数类型、指针、引用、类类型等。

例如

Input: 1/3 and 3/9
Output: 3/9 is greater than 1/3.
Input: 4/16 and 4/16
Output: 4/16 is equals to the 4/16.

我们在以下程序中使用的方案

  • 首先,我们声明两个比率

  • 然后分配两个比率的值。

  • 然后我们检查 ratio1 的值是否大于或等于 ratio2 的值。

  • 使用 ratio_greater_equal,我们可以检查:

示例

// C++ code to demonstrate the working of ratio_greater_equal
#include<iostream.h>
#include<ratio.h>
using namespace std;
int main( ){
   // Declaring ratios
   typedef ratio<10, 100> ratio1;
   typedef ratio<1, 10> ratio2;
   // Checking ratio1 is greater than or equal to ratio2.
   if (ratio_greater_equal<ratio1, ratio2>: : value )
      cout< " ratio1 is greater than or equal to ratio2";
   else
      cout<< " ratio1 is not greater than or equal to ratio2";
   cout<< "endl";
   return 0;
}

输出

如果我们运行以上代码,它将生成以下输出

10/100 is greater than or equal to 1/10
1/3 is not greater than or equal to 3/9.

更新于: 2020年2月28日

63 次查看

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.