C++ 關係運算符



關係運算符

在 C++ 中,關係運算符用於比較值或表示式。它們檢查運算元之間的關係,並以布林值(真或假)的形式返回結果。

這些比較基於諸如相等、不相等、大於、小於等條件。

關係運算符是程式語言的基本組成部分,因為它們有助於決策、迴圈和條件檢查。

關係運算符列表

以下是 C++ 中關係運算符的列表:

  • 等於 (==) − 檢查兩個值是否相等。
  • 不等於 (!=) − 檢查兩個值是否不相等。
  • 大於 (>) − 檢查左運算元是否大於右運算元。
  • 小於 (<) − 檢查左運算元是否小於右運算元。
  • 大於或等於 (>=) − 檢查左運算元是否大於或等於右運算元。
  • 小於或等於 (<=) − 檢查左運算元是否小於或等於右運算元。

關係運算符用法

關係運算符用於在 C++ 中比較兩個值或物件。在這裡,我們將看到以下列表,其中可以使用關係運算符。

  • 比較整數 − 它可以用於比較整數資料型別,如 int、long、short 等。
  • 比較浮點數 − 關係運算符也可用於比較浮點數(float、double 等)。但是,由於浮點運算的精度問題,在處理非常小或非常大的數字時,結果可能並不總是如預期的那樣。
  • 比較字元和字串 − 關係運算符根據其 ASCII 值比較字元。字串是 std::string 類的物件,關係運算符被過載以按字典順序(按字母順序)比較它們。
  • 比較物件(自定義類) − 在 C++ 中,您可以為自定義物件過載關係運算符,允許您根據某些條件比較類的例項。

關係運算符示例

以下是關係運算符的示例

#include <iostream>
#include <cmath>  // For floating-point comparison
#include <string> 

using namespace std;

// Custom class to overload relational operators
class Point {
   public:
      int x, y;
      Point(int x, int y) : x(x), y(y) {}

   bool operator>(const Point& p) { return (x*x + y*y) > (p.x*p.x + p.y*p.y); }
   bool operator==(const Point& p) { return (x == p.x && y == p.y); }
};

int main() {

   // Comparing Integers
   int a = 5, b = 10;
   cout << (a > b) << " " << (a == b) << endl; 

   // Comparing Floating-Point Numbers
   double x = 5.0, y = 5.0000001;
   cout << (fabs(x - y) < 1e-6) << endl; 

   // Comparing Characters
   char c1 = 'A', c2 = 'B';
   cout << (c1 < c2) << " " << (c1 == c2) << endl; 

   // Comparing Strings
   string str1 = "apple", str2 = "banana";
   cout << (str1 < str2) << " " << (str1 == str2) << endl; 

   // Comparing Objects
   Point p1(3, 4), p2(5, 12);
   cout << (p1 > p2) << " " << (p1 == p2) << endl;

   return 0;
}

輸出

0 0
1
1 0
1 0
0 0

關係運算符和條件語句

在 C++ 中,條件語句中的關係運算符幫助程式進行決策,並根據比較結果給出結果(真或假)。

帶有關係運算符的 if-else 語句語法

在這裡,我們將看到帶有關係運算符的 if-else 語句的語法。

if (a > b) {

   // a is greater than b
   
} else if (a == b) {

   // a is equal to b
   
} else {

   // a is less than b
   
}

帶有關係運算符的 while 迴圈語法

在這裡,我們將看到帶有關係運算符的 while 迴圈的語法。

int i = 1;
while (i <= 5) {
   if (i < 5) {
   
      // i is less than 5
	  
   } else if (i == 5) {
   
      // i is equal to 5
	  
   }
   i++;
}

關係運算符與邏輯運算子

在 C++ 中,關係運算符(>、<、==、!=、>=、<=)可以與邏輯運算子(&&、||、!)組合以形成複雜的表示式,這些表示式允許更高級別的決策。當您需要在一個表示式中檢查多個條件時,這很有幫助。

示例

#include <iostream>
using namespace std;

int main() {
   int age = 25, height = 180;

   // Check if age is between 18 and 30 and height is greater than 170
   if (age >= 18 && age <= 30 && height > 170)
   cout << "The person qualifies!" << endl;

   // Check if age is outside 18-30 or height is <= 170
   if (age < 18 || age > 30 || height <= 170)
   cout << "The person does not qualify!" << endl;

   // Check if age is NOT between 18 and 30 or height is NOT > 170
   if (!(age >= 18 && age <= 30) || !(height > 170))
   cout << "The person does not qualify due to NOT condition!" << endl;

   return 0;
}

輸出

The person qualifies!

關係運算符的限制

  • 浮點比較的精度問題(例如,由於舍入誤差,float 或 double 型別可能無法按預期工作)。
  • 比較非數值型別(例如,比較物件、字串或自定義類可能需要自定義運算子過載)。
  • 複雜表示式中的歧義,其中評估順序或運算子優先順序可能導致意外結果。
  • 在將關係運算符與混合資料型別一起使用時的型別強制轉換(例如,比較 int 和 double)。
廣告