C++程式比較兩個字串的字典序
字典序字串比較表示字串按字典順序進行比較。例如,如果存在兩個字串“apple”和“appeal”,則第一個字串將排在後面,因為前三個字元“app”相同。然後,對於第一個字串,字元是“l”,而在第二個字串中,第 4 個字元是“e”。由於“e”比“l”小,因此如果我們按字典序排列它們,它將排在前面。
在排序之前,字串會按字典序進行比較。在本文中,我們將瞭解使用 C++ 以字典序比較兩個字串的不同方法。
在 C++ 字串中使用 compare() 函式
C++ 字串物件具有 compare() 函式,該函式以另一個字串作為輸入,並將當前字串與第二個字串進行比較。當兩個字串相同時,此函式將返回 0;當第一個字串較大時,將返回負數 (-1);當第一個字串較小時,將返回正數 (+1)。
語法
<first string>.compare( <second string> )
讓我們看看 C++ 中的演算法和相應的實現。
演算法
- 將兩個字串 s 和 t 作為輸入
- cmp := 使用 s.compare() 函式,引數為 t
- 如果 cmp 為 0,則
- 這兩個字串相同
- 否則,如果 cmp 為正,則
- s 大於 t
- 否則,如果 cmp 為負,則
- s 小於 t
- 結束 if
示例
#include <iostream> using namespace std; string solve( string s, string t ){ int ret; ret = s.compare( t ); if( ret == 0 ) { return s + " and " + t + " are the same"; } else if( ret > 0 ) { return s + " is larger than " + t; } else { return s + " is smaller than " + t; } } int main(){ string s = "apple"; string t = "appeal"; cout << "The result of comparison: " << solve( s, t ) << endl; s = "popular"; t = "popular"; cout << "The result of comparison: " << solve( s, t ) << endl; s = "Hello"; t = "hello"; cout << "The result of comparison: " << solve( s, t ) << endl; }
輸出
The result of comparison: apple is larger than appeal The result of comparison: popular and popular are the same The result of comparison: Hello is smaller than hello
在類 C 字串中使用 strcmp() 函式
在 C++ 中,我們也可以使用傳統的 C 函式。C 使用字元陣列而不是 stringtype 資料。要比較兩個字串,可以使用 strcmp() 函式。此函式將兩個字串作為引數。當它們相同時返回 0。第一個字串較大時返回正值,第二個字串較大時返回負值。
語法
strcmp( <first string>, <second string> )
示例
#include <iostream> #include <cstring> using namespace std; string solve( const char* s, const char* t ){ int ret; ret = strcmp( s, t ); if( ret == 0 ) { return string(s) + " and " + string(t) + " are the same"; } else if( ret > 0 ) { return string(s) + " is larger than " + string(t); } else { return string(s) + " is smaller than " + string(t); } } int main(){ string s = "apple"; string t = "appeal"; cout << "The result of comparison: " << solve( s.c_str() , t.c_str()) << endl; s = "popular"; t = "popular"; cout << "The result of comparison: " << solve( s.c_str() , t.c_str()) << endl; s = "Hello"; t = "hello"; cout << "The result of comparison: " << solve( s.c_str() , t.c_str()) << endl; }
輸出
The result of comparison: apple is larger than appeal The result of comparison: popular and popular are the same The result of comparison: Hello is smaller than hello
使用比較運算子
與數字資料一樣,字串也可以使用比較運算子進行比較。在 C++ 中,可以將 if-else 條件直接用於字串。
語法
strcmp( <first string>, <second string> )
示例
#include <iostream> using namespace std; string solve( string s, string t ){ int ret; if( s == t ) { return s + " and " + t + " are the same"; } else if( s > t ) { return s + " is larger than " + t; } else { return s + " is smaller than " + t; } } int main(){ string s = "apple"; string t = "appeal"; cout << "The result of comparison: " << solve( s, t ) << endl; s = "popular"; t = "popular"; cout << "The result of comparison: " << solve( s, t ) << endl; s = "Hello"; t = "hello"; cout << "The result of comparison: " << solve( s, t ) << endl; }
輸出
The result of comparison: apple is larger than appeal The result of comparison: popular and popular are the same The result of comparison: Hello is smaller than hello
結論
字串比較是我們在多個應用程式中執行的一項重要任務。在 C++ 中,有幾種不同的方法可以比較字串。第一種是使用 compare() 方法。它將一個字串作為輸入並與當前字串進行檢查。在 C++ 中,比較運算子(如 (==)、(>)、(<) (<=)、(>=) 可用於字串比較。另一方面,類 C 字串可以使用 strcmp() 函式進行比較。它接受常量字元指標。當兩個字串相同時,compare() 方法和 strcmp() 方法返回 0;當第一個字串較大時,它返回正數;當第一個字串較小時,它將返回負數。