string overloaded equality and relational operators : string compare « String « C++






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








Related examples in the same category

1.String: equals
2.Compare string ignoring the case
3.Compare sub string: string4.compare( 0, string2.length(), string2 )
4.Use == > and < to compare strings
5.Use string.compare to compare two strings
6.Compare strings by index: string1.compare( 2, 5, string3, 0, 5)
7.Set with functor for string comparison
8.return true if c1 < c2 (ignoring case), false otherwise
9.Use std::lexicographical_compare to compare two char arrays
10.Compare strings
11.return true if c1 equals c2 (regardless of case), false otherwise