当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ ratio_greater_equal()用法及代码示例


ratio_greater_equal()是C++中的内置函数,可检查比率R1是否大于或等于比率R2。如果比率大于或等于比率2,则返回True;否则返回false。

用法:

template  ratio_greater_equal

模板参数该函数接受两个要比较的模板参数ratio1和ratio2。


返回值:如果ratio1大于或等于ratio2,则该函数返回一个布尔值,该值为true,否则返回false。

以下示例程序旨在说明上述函数:

示例1:

// C++ program to illustrate the 
// ratio_greater_equal function 
// when both the ratios are equal 
#include <iostream> 
#include <ratio> 
using namespace std; 
int main() 
{ 
    typedef ratio<3, 9> ratio1; 
    typedef ratio<1, 3> ratio2; 
  
    // check if R1>=R2 
    if (ratio_greater_equal<ratio1, ratio2>::value) 
        cout << "3/9 is greater than or equal to 1/3"; 
    else
        cout << "3/9 is less than 1/3"; 
  
    return 0; 
}
输出:
3/9 is greater than or equal to 1/3

示例2:

// C++ program to illustrate the 
// ratio_greater_equal function 
// to show greater than 
#include <iostream> 
#include <ratio> 
using namespace std; 
int main() 
{ 
    typedef ratio<1, 2> ratio1; 
    typedef ratio<1, 3> ratio2; 
  
    // check if R1>=R2 
    if (ratio_greater_equal<ratio1, ratio2>::value) 
        cout << "1/2 is greater than or equal to 1/3"; 
    else
        cout << "1/2 is less than 1/3"; 
  
    return 0; 
}
输出:
1/2 is greater than or equal to 1/3

示例3:

// C++ program to illustrate the 
// ratio_greater_equal function 
// to show when less than 
#include <iostream> 
#include <ratio> 
using namespace std; 
int main() 
{ 
    typedef ratio<1, 10> ratio1; 
    typedef ratio<1, 8> ratio2; 
  
    // check if R1>=R2 
    if (ratio_greater_equal<ratio1, ratio2>::value) 
        cout << "1/10 is greater than or equal to 1/8"; 
    else
        cout << "1/10 is less than 1/8"; 
  
    return 0; 
}
输出:
1/10 is less than 1/8


相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 ratio_greater_equal() function in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。