C++ の std::string::compare()

比較する() 文字列クラスのパブリックメンバー関数です。文字列オブジェクト (または部分文字列) の値を、引数で指定された文字のシーケンスと比較します。
Compare() は各文字列に対して複数の引数を処理できるため、インデックスと長さによって部分文字列を指定できます。
戻り値の型: Compare() はブール値ではなく整数値を返します。
string::compare() のさまざまな構文:

    構文 1: 文字列 *this と文字列 str を比較します。
 int string::compare (const string& str) const Returns: 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 : *これが str より長いか、一致しない最初の文字の方が大きい場合>>'   

CPP




// CPP code for demonstrating> // string::compare (const string& str) const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> > // returns a value <0 (s1 is smaller than s2)> > if> ((s1.compare(s2)) <0)> > cout < < s1 < < ' is smaller than ' < < s2 < < endl;> > // returns 0(s1, is being compared 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: インデックス idx から始まる文字列 *this の最大 len 文字と文​​字列 str を比較します。
 int string::compare (size_type idx, size_type len, const string& str) const Throws out_of_range if index>サイズ()。>>   

CPP




// CPP code to demonstrate> // int string::compare (size_type idx, size_type len,> // const string& str) const> #include> 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: インデックス idx で始まる string *this の最大 len 文字と、インデックス 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>サイズ()。 str_idx> str.size() の場合、out_of_range をスローします。> 

CPP




// 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> 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 techcodeview.com World 
    構文 4: 文字列 *this の文字と C 文字列 cstr の文字を比較します。
 int string::compare (const char* cstr) const 

CPP




// CPP code to demonstrate> // int string::compare (const char* cstr) const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> > // returns <0 (s1 < 'techcodeview.com')> > if> ((s1.compare('techcodeview.com')) <0)> > cout < < s1 < < ' is smaller than string ' < < 'techcodeview.com';> > //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 techcodeview.com forGeeks is equal to string forGeeks 
    構文 5: インデックス idx から始まる文字列 *this の最大 len 文字と C 文字列 cstr のすべての文字を比較します。
 int string::compare (size_type idx, size_type len, const char* cstr) const 

cstr はヌル ポインター (NULL) ではないことに注意してください。

CPP




// CPP code to demonstrate> // int string::compare (size_type idx, size_type len,> // const char* cstr) const> #include> 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: インデックス idx から始まる string *this の最大 len 文字を、文字配列 chars の chars_len 文字と比較します。
 int string::compare (size_type idx, size_type len, const char* chars,  size_type chars_len)const 

chars には少なくとも chars_len 文字が必要であることに注意してください。文字には任意の値を指定できます。したがって、「 」には特別な意味はありません。

CPP




// CPP code to demonstrate> // int string::compare (size_type idx, size_type len,> // const char* chars, size_type chars_len)const> #include> 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;> }>