C++ 中的 ratio_not_equal() 及其示例
在本文中,我們將討論 C++ STL 中 ratio_not_equal 模板的工作原理、語法和示例。
什麼是 ratio_not_equal 模板?
ratio_not_equal 模板是 C++ STL 中內建的,定義在 <ratio> 標頭檔案中。ratio_not_equal 用於比較不相等的兩個比率。此模板接受兩個引數並檢查給定的比率是否不應相等。比如我們有兩個比率 1/2 和 3/9,它們不相等,因此對於給定的模板,它們成立。當兩個比率不相等時,此函式返回 true。
因此,當我們想檢查兩個比率是否不等時,可以利用提供的模板(簡化了編碼)代替用 C++ 編寫整個邏輯。
語法
template <class ratio1, class ratio2> ratio_not_equal;
引數
該模板接受以下引數 −
ratio1, ratio2 − 這是我們想要檢查它們是否相等的兩個比率。
返回值
當兩個比率不相等時,此函式返回 true,否則,當兩個比率相等時,此函式返回 false。
輸入
typedef ratio<3, 6> ratio1; typedef ratio<1, 2> ratio2; ratio_not_equal<ratio1, ratio2>::value;
輸出
false
輸入
typedef ratio<3, 9> ratio1; typedef ratio<1, 2> ratio2; ratio_not_equal<ratio1, ratio2>::value;
輸出
true
示例
#include <iostream>
#include <ratio>
using namespace std;
int main(){
typedef ratio<2, 5> R_1;
typedef ratio<1, 3> R_2;
//check whether ratios are equal or not
if (ratio_not_equal<R_1, R_2>::value)
cout<<"Ratio 1 and Ratio 2 aren't equal";
else
cout<<"Ratio 1 and Ratio 2 are equal";
return 0;
}輸出
如果我們執行上面的程式碼,他會生成如下的輸出 −
Ratio 1 and Ratio 2 aren't equal
示例
#include <iostream>
#include <ratio>
using namespace std;
int main(){
typedef ratio<2, 5> R_1;
typedef ratio<2, 5> R_2;
//check whether ratios are equal or not
if (ratio_not_equal<R_1, R_2>::value)
cout<<"Ratio 1 and Ratio 2 aren't equal";
else
cout<<"Ratio 1 and Ratio 2 are equal";
return 0;
}輸出
如果我們執行上面的程式碼,他會生成如下的輸出 −
Ratio 1 and Ratio 2 aren equal
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP