C++ Tutorial/STL Algorithms Sorting/lexicographical compare

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Illustrating the generic lexicographical_compare algorithm

<source lang="cpp">#include <iostream>

  1. include <cassert>
  2. include <algorithm>
  3. include <vector>

using namespace std;

int main() {

 string s("helio");
 string s2("hello");
 vector<char> vector1(s.begin(), s.end());
 vector<char> vector2(s.begin(), s.end());
 // Show that vector1 is lexicographically less than vector2:
 bool result = lexicographical_compare(vector1.begin(),vector1.end(), vector2.begin(), vector2.end());
 cout << result;
 return 0;

}</source>

0

Use std::lexicographical_compare to compare two char arrays

<source lang="cpp">#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;

}</source>

HELLO is greater than or equal to BYE BYE