C++ 字串比較


C++ 字串比較是指根據詞典規則評估兩個字串以確定它們是否相等或它們的順序的過程。

字串比較可以使用內建運算子(例如==!=<>)或compare() 方法來完成。但預設情況下,這些比較區分大小寫,這意味著“tutorial point”和“Tutorial point”被認為是不同的。字串比較在執行排序、搜尋和輸入驗證等任務中起著重要作用。

C++ 中的字串型別

基本上,C++ 中主要有兩種型別的字串:

  • C 風格字串 − C++ 中的這種字串是帶有空字元('\0') 的字元陣列。
  • std::string − 這種字串是 C++ 標準庫的一部分,它提供了一種更強大且使用者友好的方式來處理字串,因為它自動管理記憶體,允許動態調整大小,並提供大量用於操作的成員函式,例如連線、子串提取和搜尋。

比較 C 風格字串

以下是比較 C 風格字串的方法:

1. strcmp()

您可以使用strcmp() 函式(來自<cstring> 庫)來比較兩個字串。

示例

以下是一個比較兩個 C 風格字串的示例:

#include <iostream>
#include <cstring>

int main() {
   const char* str1 = "hello";
   const char* str2 = "Tutorialspoint Learner";

   int result = strcmp(str1, str2);

   if (result < 0) {
      std::cout << "str1 is less than str2\n";
   } else if (result > 0) {
      std::cout << "str1 is greater than str2\n";
   } else {
      std::cout << "str1 is equal to str2\n";
   }

   return 0;
}

輸出

str1 is greater than str2

解釋

  • strcmp 函式比較兩個字串,並返回負數 (str1 < str2)、零 (str1 = str2) 和正數 (str1 > str2)。
  • 在這種情況下,“hello”在詞典順序上大於“Tutorialspoint Learner”,因此輸出為“str1 大於 str2”。
  • 在詞典順序中,字串是基於其 ASCII 值逐個字元進行比較的,其中 'h' (ASCII 104) 和 'T' (ASCII 84)
  • 所以 104 > 84,因此此比較導致“hello”大於“Tutorialspoint Learner”。

2. strcoll()

strcoll() 函式 根據當前區域設定比較兩個 C 字串,這對於國際化很有用。它的行為類似於 strcmp(),但考慮了特定於區域設定的規則。

示例

#include <iostream>
#include <cstring>
#include <locale>

int main() {
   const char* str1 = "hello";
   const char* str2 = "Tutorialspoint Learner";

   // Set the locale (optional, depends on your environment)
   std::setlocale(LC_COLLATE, "en_US.UTF-8");

   int result = strcoll(str1, str2);

   if (result < 0) {
      std::cout << "str1 is less than str2\n";
   } else if (result > 0) {
      std::cout << "str1 is greater than str2\n";
   } else {
      std::cout << "str1 is equal to str2\n";
   }

   return 0;
}

輸出

str1 is greater than str2

解釋

  • std::setlocale() 函式將字串排序 (比較) 的區域設定設定為 "en_US.UTF-8",這是美國英語區域設定。
  • strcoll() 函式根據當前區域設定比較 str1 和 str2。
  • 如果 str1 < str2,則返回“-ve”值;如果它們相等,則返回 0;如果 str1 > str2,則返回“+ve”值。
  • 由於 'h' (ASCII 104) > 'T' (ASCII 84),因此輸出為 'h' 大於 'T'。

比較 std::string

在 C++ 中,比較std::string 物件有各種方法,這些方法提供了訪問其相等性或相對順序的不同方法。

1. 比較運算子

比較運算子可以直接比較兩個std::string 物件。

此比較也是按詞典順序進行的,這意味著字元是根據其 ASCII 值進行比較的。以下是以下內容:

  • == (相等)
  • != (不相等)
  • < (小於)
  • > (大於)
  • <= (小於或等於)
  • >= (大於或等於)

示例

#include <iostream>
#include <string>

int main() {
   std::string str1 = "Allen";
   std::string str2 = "allen";

   std::cout << (str1 == str2) << std::endl; // false (0)
   std::cout << (str1 != str2) << std::endl; // true (1)
   std::cout << (str1 < str2) << std::endl;  // true (1)
   std::cout << (str1 > str2) << std::endl;  // false (0)

   return 0;
}

輸出

0
1
1
0

解釋

由於A (65) 的 ASCII 值小於a (97),因此相應地接收輸出。

2. std::string::compare() 方法

std::string::compare() 方法也用於比較兩個字串的值。它根據詞典比較返回一個整數值。

  • 0:如果兩個字串相等
  • > 0 (+ve):如果第一個字串更大
  • < 0 (-ve):如果第一個字串更小

示例

#include <iostream>
#include <string>

int main() {
   std::string str1 = "apple";
   std::string str2 = "banana";

   std::cout << str1.compare(str2) << std::endl; 
   std::cout << str1.compare("apple") << std::endl; 

   return 0;
}

輸出

-1
0

解釋

  • 首先,由於 'a' (ASCII 97) 小於 'b' (ASCII 98),所以輸出為負數 (-1)。
  • 其次,輸出為 0,因為儲存在 str1 和“apple”中的值相同。
廣告