字串的比較

2021-09-25 18:11:57 字數 843 閱讀 8170

1)我們比較常用的是比較運算子

string 類的常見運算子包括 >、<、==、>=、<=、!=。

示例**:

#include #include using namespace std;

int main ()

讀者應注意,對於參加比較的兩個字串,任乙個字串均不能為 null,否則程式會異常退出。

2)basic_string 類模板提供了 compare() 函式,支援多引數處理。該函式返回乙個整數來表示比較結果。如果相比較的兩個子串相同,compare() 函式返回 0,否則返回非零值。

compare() 的原型如下:

int compare (const basic_string& s) const;

int compare (const ch* p) const;

int compare (size_type pos, size_type n, const basic_string& s) const;

int compare (size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const;

int compare (size_type pos, size_type n, const ch* p, size_type = npos) const;

示例**:

#include #include using namespace std;

int main ()

執行結果為: m = 1, n = -1

字串的比較

給定兩個僅由大寫字母或小寫字母組成的字串 長度介於1到10之間 它們之間的關係是以下4中情況之一 1 兩個字串長度不等。比如 beijing 和 hebei 2 兩個字串不僅長度相等,而且相應位置上的字元完全一致 區分大小寫 比如 beijing 和 beijing 3 兩個字串長度相等,相應位置上...

字串的比較

兩個字串比較,是將這兩個字串從左到右逐個字元比較。1.如果兩個字串長度相等,且每乙個相應位置上的字元都一樣,就認為兩個字串相等,如 abc abc 2.如果兩個字串長度不相等,則以第乙個不相同的字元作為基準,而不考慮他們的長度及其後字元的比較結果。如 abh abfc000 3.如果兩個字串長度不相...

比較字串

其實這是在 c 從入門到精通 上摘抄的,由於不知道漢字怎麼排序,就查了查書,寫在這裡也可以幫助其他初學者更方便的找到漢字的排序方法。用於排序 int compare string str1,string str2 int compare string str1,string str2,bool ig...