Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::lexicographical_compare

 

std::lexicographical_compare is an STL algorithm to determine if two sequences of characters are in an alphabetically sorted order. Note that std::strings are compared this way by default.

 

#include <algorithm>
#include <cassert>
#include <string>

int main()
{
  const std::string s1 = "Bilderbikkel";
  const std::string s2 = "Parachutemeisje";

  //Assume Bilderbikkel comes before Parachutemeisje
  //with std::lexicographical_compare
  assert(std::lexicographical_compare(
    s1.begin(),s1.end(),s2.begin(),s2.end()) == true);

  //Assume Bilderbikkel comes before Parachutemeisje
  //with default std::string behavior
  assert(s1 < s2);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict