字典序字串比較是指字串依照字典順序比較。例如,如果有兩個字串'apple'和'appeal',第一個字串將排在後面,因為前三個字元'app'是相同的。然後對於第一個字串,字元是'l',而在第二個字串中,第四個字元是'e'。由於'e'比'l'短,所以如果我們按照字典順序排列,它將排在前面。
在安排之前,字串會依照字典順序進行比較。在本文中,我們將看到 使用C 進行以字典順序比較兩個字串的不同技術。
C string物件有一個compare()函數,它接受另一個字串作為輸入並進行比較。
比較目前字串與第二個字串。當兩個字串相同時,此函數將傳回0 字串相同時,當第一個字串較大時,它將傳回負數(-1) 當第一個字串較小時,將其翻譯為中文:當第一個字串較小時,為正數( 1)。
<first string>.compare( <second string> )
讓我們來看看C 中的演算法和對應的實作。
#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 中,我們也可以使用傳統的C函數。 C使用字元數組而不是字串類型。
data. To compare two strings the strcmp() functions are used. This function takes two 將字串作為參數。當它們相同時返回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
就像數字資料一樣,字串也可以使用比較運算子進行比較。 if-else conditions can be used directly for strings in C .
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()方法 需要翻譯的內容為:Which takes one string as input and checks with the current string. In C the comparison 運算子如(==)、(>)、(=)可以用於字串比較。另一方面, C-like字串可以使用strcmp()函數來比較。此函數接受常數 character pointers. The compare() method and the strcmp() method returns 0 when both 第一個字串較大時,傳回一個正數;當兩個字串相同時,傳回0 第一個較小,它將傳回一個正數。
以上是C++程式比較兩個字串的字典序的詳細內容。更多資訊請關注PHP中文網其他相關文章!