C++/String/string compare — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Версия 14:21, 25 мая 2010

Compare string ignoring the case

  
 
#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!
 */


Compare strings

  
#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;
}


Compare strings by index: string1.compare( 2, 5, string3, 0, 5)

  
#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
 */


Compare sub string: string4.compare( 0, string2.length(), string2 )

  
 
#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
 */


return true if c1

  
#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( )
{
}


return true if c1 equals c2 (regardless of case), false otherwise

  
#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( )
{
}


Set with functor for string comparison

  
 
#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
*/


String: equals

  
 
#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
*/


string overloaded equality and relational operators

  
 
#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
 */


Use == > and

  
 

#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
 */


Use std::lexicographical_compare to compare two char arrays

  
 
#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
 */


Use string.compare to compare two strings

  
 

#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
 */