Use std::lexicographical_compare to compare two char arrays : string compare « String « C++






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

 */
        
    
  








Related examples in the same category

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