C++/String/string compare
Содержание
- 1 Compare string ignoring the case
- 2 Compare strings
- 3 Compare strings by index: string1.compare( 2, 5, string3, 0, 5)
- 4 Compare sub string: string4.compare( 0, string2.length(), string2 )
- 5 return true if c1
- 6 return true if c1 equals c2 (regardless of case), false otherwise
- 7 Set with functor for string comparison
- 8 String: equals
- 9 string overloaded equality and relational operators
- 10 Use == > and
- 11 Use std::lexicographical_compare to compare two char arrays
- 12 Use string.compare to compare two strings
Compare string ignoring the case
<source lang="cpp">
- include <string>
- include <iostream>
- include <algorithm>
- include <cctype>
- include <cwctype>
using namespace std; inline bool caseInsCharCompareN(char a, char b) {
return(toupper(a) == toupper(b));
}
bool caseInsCompare(const string& s1, const string& s2) {
return((s1.size( ) == s2.size( )) && equal(s1.begin( ), s1.end( ), s2.begin( ), caseInsCharCompareN));
} int main( ) {
string s1 = "In the BEGINNING..."; string s2 = "In the beginning..."; if (caseInsCompare(s1, s2)) cout << "Equal!\n";
} /* Equal!
*/ </source>
Compare strings
<source lang="cpp">
- include <string>
- include <iostream>
using namespace std; int main(int argc, char** argv) {
string myString = "hello"; myString += ", there"; string str2 = myString; if (myString == str2) { str2[0] = "H"; } cout << myString << endl; cout << str2 << endl;
}
</source>
Compare strings by index: string1.compare( 2, 5, string3, 0, 5)
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl;
- include <string>
using std::string; int main() {
string string1( "AAAAAAAAAAAAAA" ); string string2( "BBBBBBBBBBBBBB" ); string string3( "CCCCCCCCCCCCCC" ); string string4( string2 ); cout << "string1: " << string1 << "\nstring2: " << string2 << "\nstring3: " << string3 << "\nstring4: " << string4 << "\n\n"; // comparing string1 (elements 2-5) and string3 (elements 0-5) int result = string1.compare( 2, 5, string3, 0, 5 ); if ( result == 0 ) cout << "string1.compare( 2, 5, string3, 0, 5 ) == 0\n"; else { if ( result > 0 ) cout << "string1.compare( 2, 5, string3, 0, 5 ) > 0\n"; else cout << "string1.compare( 2, 5, string3, 0, 5 ) < 0\n"; } return 0;
} /* string1: AAAAAAAAAAAAAA string2: BBBBBBBBBBBBBB string3: CCCCCCCCCCCCCC string4: BBBBBBBBBBBBBB string1.compare( 2, 5, string3, 0, 5 ) < 0
*/ </source>
Compare sub string: string4.compare( 0, string2.length(), string2 )
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl;
- include <string>
using std::string; int main() {
string string1( "AAAAAAAAAAAAAA" ); string string2( "BBBBBBBBBBBBBB" ); string string3( "CCCCCCCCCCCCCC" ); string string4( string2 ); cout << "string1: " << string1 << "\nstring2: " << string2 << "\nstring3: " << string3 << "\nstring4: " << string4 << "\n\n"; // comparing string2 and string4 int result = string4.compare( 0, string2.length(), string2 ); if ( result == 0 ) cout << "string4.compare( 0, string2.length(), " << "string2 ) == 0" << endl; else { if ( result > 0 ) cout << "string4.compare( 0, string2.length(), " << "string2 ) > 0" << endl; else cout << "string4.compare( 0, string2.length(), " << "string2 ) < 0" << endl; } return 0;
} /* string1: AAAAAAAAAAAAAA string2: BBBBBBBBBBBBBB string3: CCCCCCCCCCCCCC string4: BBBBBBBBBBBBBB string4.compare( 0, string2.length(), string2 ) == 0
*/ </source>
return true if c1
<source lang="cpp">
- include <algorithm>
- include <cctype>
- include <iostream>
- include <string>
using namespace std; // return true if c1 < c2 (ignoring case), false otherwise bool less_than_insensitive( char c1, char c2 ) {
return tolower( static_cast<unsigned char>( c1 ) ) < tolower( static_cast<unsigned char>( c2 ) );
} int main( ) { }
</source>
return true if c1 equals c2 (regardless of case), false otherwise
<source lang="cpp">
- include <algorithm>
- include <cctype>
- include <iostream>
- include <string>
using namespace std; // return true if c1 equals c2 (regardless of case), false otherwise bool equal_to_insensitive( char c1, char c2 ){
return tolower( static_cast<unsigned char>( c1 ) ) == tolower( static_cast<unsigned char>( c2 ) );
} int main( ) { }
</source>
Set with functor for string comparison
<source lang="cpp">
- include <iostream>
- include <set>
- include <string>
- include <functional>
- include <cassert>
using namespace std; struct strPtrLess {
bool operator( )(const string* p1,const string* p2) { return(*p1 < *p2); }
}; int main( ) {
set<string*, strPtrLess> setStrPtr; // less-than functor string s1 = "T"; string s2 = "D"; string s3 = "H"; setStrPtr.insert(&s1); setStrPtr.insert(&s2); setStrPtr.insert(&s3); for (set<string*, strPtrLess>::const_iterator p = setStrPtr.begin( ); p != setStrPtr.end( ); ++p) cout << **p << endl;
} /* D H T
- /
</source>
String: equals
<source lang="cpp">
- include <iostream.h>
- include <string>
using std::string; main(void) {
string s1 = "abcdefghijk", s2 = "1234567890", s3,s4,s5; s3=s1+s2; cout << s3 <<endl; s4=s3; if (s4==s3) cout << " s4==s3 is true\n"; return(0);
} /* abcdefghijk1234567890
s4==s3 is true
- /
</source>
string overloaded equality and relational operators
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl;
- include <string>
using std::string; int main() {
string s1( "AA" ); string s2( " AAB" ); string s3; // cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 << "\"; s3 is \"" << s3 << "\"" << "\n\nThe results of comparing s2 and s1:" << "\ns2 == s1 yields " << ( s2 == s1 ? "true" : "false" ) << "\ns2 != s1 yields " << ( s2 != s1 ? "true" : "false" ) << "\ns2 > s1 yields " << ( s2 > s1 ? "true" : "false" ) << "\ns2 < s1 yields " << ( s2 < s1 ? "true" : "false" ) << "\ns2 >= s1 yields " << ( s2 >= s1 ? "true" : "false" ) << "\ns2 <= s1 yields " << ( s2 <= s1 ? "true" : "false" ); return 0;
} /* s1 is "AA"; s2 is " AAB"; s3 is "" The results of comparing s2 and s1: s2 == s1 yields false s2 != s1 yields true s2 > s1 yields false s2 < s1 yields true s2 >= s1 yields false s2 <= s1 yields true
*/ </source>
Use == > and
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl;
- include <string>
using std::string; int main() {
string string1( "AAAAAAAAAAAAAA" ); string string2( "BBBBBBBBBBBBBB" ); string string3( "CCCCCCCCCCCCCC" ); string string4( string2 ); cout << "string1: " << string1 << "\nstring2: " << string2 << "\nstring3: " << string3 << "\nstring4: " << string4 << "\n\n"; // comparing string1 and string4 if ( string1 == string4 ) cout << "string1 == string4\n"; else{ if ( string1 > string4 ) cout << "string1 > string4\n"; else // string1 < string4 cout << "string1 < string4\n"; } return 0;
} /* string1: AAAAAAAAAAAAAA string2: BBBBBBBBBBBBBB string3: CCCCCCCCCCCCCC string4: BBBBBBBBBBBBBB string1 < string4
*/ </source>
Use std::lexicographical_compare to compare two char arrays
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl;
- include <algorithm>
- include <vector>
- include <iterator>
int main() {
char c1[ 10 ] = "HELLO"; char c2[ 10 ] = "BYE BYE"; // perform lexicographical comparison of c1 and c2 bool result = std::lexicographical_compare( c1, c1 + 10, c2, c2 + 10 ); cout << c1 << ( result ? " is less than " : " is greater than or equal to " ) << c2 << endl; return 0;
} /* HELLO is greater than or equal to BYE BYE
*/ </source>
Use string.compare to compare two strings
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl;
- include <string>
using std::string; int main() {
string string1( "AAAAAAAAAAAAAA" ); string string2( "BBBBBBBBBBBBBB" ); string string3( "CCCCCCCCCCCCCC" ); string string4( string2 ); cout << "string1: " << string1 << "\nstring2: " << string2 << "\nstring3: " << string3 << "\nstring4: " << string4 << "\n\n";
// comparing string1 and string2 int result = string1.compare( string2 ); if ( result == 0 ) cout << "string1.compare( string2 ) == 0\n"; else { if ( result > 0 ) cout << "string1.compare( string2 ) > 0\n"; else cout << "string1.compare( string2 ) < 0\n"; } return 0;
} /* string1: AAAAAAAAAAAAAA string2: BBBBBBBBBBBBBB string3: CCCCCCCCCCCCCC string4: BBBBBBBBBBBBBB string1.compare( string2 ) < 0
*/ </source>