C++ 中哪些是相等運算子?


C++ 中的相等性運算子為“等於”(==) 和“不等於”(!=)。它們的作用與它們的名稱一致。二元相等性運算子將它們的運算數與嚴格的相等或不相等進行比較。等於 (==) 和不等於 (!=) 的相等性運算子的優先順序低於關係運算符,但它們的行為類似。這些運算子的結果型別為布林值。

等於運算子 (==) 在兩個運算數具有相同的值時返回真 (1);否則返回假 (0)。如果不等於運算子 (!=),則在運算數的值不相同時返回真;否則返回假。 

示例

#include <iostream>  
using namespace std;  

int main() {  
   cout  << boolalpha  
   // For printing true and false as true and false in case of a bool result
 
   << "The true expression 3 != 2 yields: "  
   << (3 != 2) << endl  
   << "The false expression 20 == 10 yields: "  
   << (20 == 10) << endl;  
}

輸出

這會產生如下輸出 −

The true expression 3 != 2 yields: true
The false expression 20 == 10 yields: false

更新於: 2020-02-11

1 千次+

職業生涯起航

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.