Use std::lexicographical_compare to compare two char arrays : lexicographical_compare « STL Algorithms Sorting « 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.Illustrating the generic lexicographical_compare algorithm