boost::range::lexicographical_compare

References

Headers

boost::range::lexicographical_compare is available by including any of the following headers:

  • boost/range/algorithm/lexicographical_compare.hpp or
  • boost/range/algorithm.hpp

Examples

lexicographical_compare.cpp

#include <functional>
#include <iostream>
#include <locale>
#include <boost/range/algorithm.hpp>

bool iless(char lhs, char rhs) {
    std::locale loc;
    return std::toupper(lhs, loc) < std::toupper(rhs, loc);
}

int main() {
    std::string s1 = "abc";
    std::string s2 = "ABD";
    std::string s3 = "ab";
    std::vector<int> vec = {'c', 'c'};

    // Compare two ranges lexicographically, by applying operator<(), or a
    // supplied binary predicate to each pair of elements.
    // Can be used as a less-than operator for ranges.
    bool lt_s1_s2 = boost::range::lexicographical_compare(s1, s2);
    bool lt_s1_s3 = boost::range::lexicographical_compare(s1, s3);
    bool lt_s1_vec = boost::range::lexicographical_compare(s1, vec);
    bool ilt_s1_s2 = boost::range::lexicographical_compare(s1, s2, iless);

    std::cout << "lexicographical_compare(s1, s2): " << (lt_s1_s2 ? "true" : "false") << std::endl;
    std::cout << "lexicographical_compare(s1, s3): " << (lt_s1_s3 ? "true" : "false") << std::endl;
    std::cout << "lexicographical_compare(s1, vec): " << (lt_s1_vec ? "true" : "false") << std::endl;
    std::cout << "lexicographical_compare(s1, s2, iequals): " << (ilt_s1_s2 ? "true" : "false")
              << std::endl;
    return 0;
}

Output:

lexicographical_compare(s1, s2): false
lexicographical_compare(s1, s3): false
lexicographical_compare(s1, vec): true
lexicographical_compare(s1, s2, iequals): true

 

Boost Range for Humans

This reference is part of Boost Range for Humans. Click the link to the overview.