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


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 的值,則此函數將返回布爾值,為真,即整數位 1,否則將返回假,即整數位 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.

相關用法


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