C++/String/string compare

Материал из C\C++ эксперт
Версия от 13:25, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Compare string ignoring the case

<source lang="cpp">

  1. include <string>
  2. include <iostream>
  3. include <algorithm>
  4. include <cctype>
  5. 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">

  1. include <string>
  2. 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">

  1. include <iostream>

using std::cout; using std::endl;

  1. 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">

  1. include <iostream>

using std::cout; using std::endl;

  1. 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">

  1. include <algorithm>
  2. include <cctype>
  3. include <iostream>
  4. 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">

  1. include <algorithm>
  2. include <cctype>
  3. include <iostream>
  4. 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">

  1. include <iostream>
  2. include <set>
  3. include <string>
  4. include <functional>
  5. 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">

  1. include <iostream.h>
  2. 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">

  1. include <iostream>

using std::cout; using std::endl;

  1. 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">


  1. include <iostream>

using std::cout; using std::endl;

  1. 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">

  1. include <iostream>

using std::cout; using std::endl;

  1. include <algorithm>
  2. include <vector>
  3. 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">


  1. include <iostream>

using std::cout; using std::endl;

  1. 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>