compare()是字符串類的公共成員函數。它將字符串對象(或子字符串)的值與其參數指定的字符序列進行比較。
compare()可以為每個字符串處理多個參數,以便可以通過其索引和長度指定子字符串。
返回類型:compare()返回一個整數值,而不是布爾值。
string::compare(的不同語法):
- 語法1:比較字符串* this和字符串str。
int string::compare (const string& str) const 返回: 0: if both strings are equal. A value < 0:if *this is shorter than str or, first character that doesn't match is smaller than str. A value > 0: if *this is longer than str or, first character that doesn't match is greater
// CPP code for demonstrating // string::compare (const string& str) const #include<iostream> using namespace std; void compareOperation(string s1, string s2) { // returns a value < 0 (s1 is smaller then s2) if((s1.compare(s2)) < 0) cout << s1 << " is smaller than " << s2 << endl; // returns 0(s1, is being comapared to itself) if((s1.compare(s1)) == 0) cout << s1 << " is equal to " << s1 << endl; else cout << "Strings didn't match "; } // Driver Code int main() { string s1("Geeks"); string s2("forGeeks"); compareOperation(s1, s2); return 0; }
輸出:
Geeks is smaller than forGeeks Geeks is equal to Geeks
- 語法2:最多比較字符串* this的len個字符,以索引idx和字符串str開頭。
int string::compare (size_type idx, size_type len, const string& str) const Throws out_of_range if index > size().
// CPP code to demonstrate // int string::compare (size_type idx, size_type len, // const string& str) const #include<iostream> using namespace std; void compareOperation(string s1, string s2) { // Compares 5 characters from index number 3 of s2 with s1 if((s2.compare(3, 5, s1)) == 0) cout << "Here, "<< s1 << " are " << s2; else cout << "Strings didn't match "; } // Driver Code int main() { string s1("Geeks"); string s2("forGeeks"); compareOperation(s1, s2); return 0; }
輸出:
Here, Geeks are forGeeks
-
語法3:比較最多*個字符串的len個字符,此字符以索引idx開頭,最多比較以字符串str_idx開頭的字符串str的str_len個字符。
int string::compare (size_type idx, size_type len, const string& str, size_type str_idx, size_type str_len) const Throws out_of_range if idx > size(). Throws out_of_range if str_idx > str.size().
// CPP code to demonstrate // int string::compare (size_type idx, size_type len, const string& // str, size_type str_idx, size_type str_len) const #include<iostream> using namespace std; void compareOperation(string s1, string s2) { // Compares 5 characters from index number 0 of s1 with // 5 characters from index 3 of s2 if((s1.compare(0, 5, s2, 3, 5)) == 0) cout << "Welcome to " << s1 << s2 << " World"; else cout << "Strings didn't match "; } // Driver Code int main() { string s1("Geeks"); string s2("forGeeks"); compareOperation(s1, s2); return 0; }
輸出:
Welcome, to GeeksforGeeks World
-
語法4:將字符串* this的字符與C-string cstr的字符進行比較。
int string::compare (const char* cstr) const
// CPP code to demonstrate // int string::compare (const char* cstr) const #include<iostream> using namespace std; void compareOperation(string s1, string s2) { // returns < 0 (s1 < "GeeksforGeeks") if((s1.compare("GeeksforGeeks")) < 0) cout << s1 << " is smaller than string " << "GeeksforGeeks"; //returns 0 (s2 is "forgeeks") if((s2.compare("forGeeks")) == 0) cout << endl << s2 << " is equal to string " << s2; else cout << "Strings didn't match "; } // Driver Code int main() { string s1("Geeks"); string s2("forGeeks"); compareOperation(s1, s2); return 0; }
輸出:
Geeks is smaller than string GeeksforGeeks forGeeks is equal to string forGeeks
-
語法5:最多比較字符串* this的len個字符,從索引idx到C-string cstr的所有字符。
int string::compare (size_type idx, size_type len, const char* cstr) const
請注意,cstr可能不是空指針(NULL)。
// CPP code to demonstrate // int string::compare (size_type idx, size_type len, // const char* cstr) const #include<iostream> using namespace std; void compareOperation(string s1) { // Compares 5 characters from 0 index of s1 with "Geeks" if((s1.compare(0, 5, "Geeks")) == 0) cout << s1 << " are " << "awesome people"; else cout << "Strings didn't match "; } // Driver Code int main() { string s1("Geeks"); compareOperation(s1); return 0; }
輸出:
Geeks are awesome people
-
語法6:最多比較字符串* this的len個字符,從索引idx開始,以字符數組chars的chars_len字符開頭。
int string::compare (size_type idx, size_type len, const char* chars, size_type chars_len)const
請注意,字符必須至少包含chars_len字符。字符可以具有任意值。因此,“ \ 0”沒有特殊含義。
// CPP code to demonstrate // int string::compare (size_type idx, size_type len, // const char* chars, size_type chars_len)const #include<iostream> using namespace std; void compareOperation(string s1, string s2) { // Compares 5 characters from 0 index of s1 with // 5 characters of string "Geeks" if((s1.compare(0, 5, "Geeks", 5)) == 0) cout << "This is " << s1 << s2 ; else cout << "Strings didn't match "; } // Driver Code int main() { string s1("Geeks"); string s2("forGeeks"); compareOperation(s1, s2); return 0; }
輸出:
This is GeeksforGeeks
相關用法
注:本文由純淨天空篩選整理自 std::string::compare() in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。