當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ ratio_greater()用法及代碼示例

ratio_greater()是C++中的內置函數,可檢查比率R1是否大於比率R2。如果比率大於比率2,則返回True;否則返回false。

用法:

template  ratio_greater

模板參數該函數接受兩個要比較的模板參數ratio1和ratio2。


返回值:該函數返回一個布爾值,如果ratio1大於ratio2,則為true,否則返回false。

以下示例程序旨在說明上述函數:

示例1:

// C++ program to illustrate the 
// ratio_greater function 
#include <iostream> 
#include <ratio> 
using namespace std; 
int main() 
{ 
    typedef ratio<7, 7> ratio1; 
    typedef ratio<4, 7> ratio2; 
  
    // check if R1>R2 
    if (ratio_greater<ratio1, ratio2>::value) 
        cout << "7/7 is more than 4/7"; 
    else
        cout << "7/7 is not more than 4/7"; 
  
    return 0; 
}
輸出:
7/7 is more than 4/7

示例2:

// C++ program to illustrate the 
// ratio_greater function 
#include <iostream> 
#include <ratio> 
using namespace std; 
int main() 
{ 
    typedef ratio<1, 2> ratio1; 
    typedef ratio<4, 7> ratio2; 
  
    // check if R1>R2 
    if (ratio_greater<ratio1, ratio2>::value) 
        cout << "1/2 is more than 4/7"; 
    else
        cout << "1/2 is not more than 4/7"; 
  
    return 0; 
}
輸出:
1/2 is not more than 4/7


相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 ratio_greater() function in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。