boost::range::lower_bound

References

Headers

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

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

Examples

lower_bound.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);
}

void lower_bound_without_predicate() {
    std::string s = "accceg";

    // Return an iterator pointing to the first element that is greater
    // or equal to the value argument.
    // Requires a sorted (or at least partitioned) input range.
    std::string::iterator it_b = boost::range::lower_bound(s, 'b');
    std::string::iterator it_c = boost::range::lower_bound(s, 'c');

    std::cout << "Index of lower_bound(s, 'b'): " << (it_b - s.begin()) << std::endl;
    std::cout << "Index of lower_bound(s, 'c'): " << (it_c - s.begin()) << std::endl;
}

void lower_bound_with_predicate() {
    std::string s = "aCcCEg";

    // An overload that takes an ordering predicate is also available.
    std::string::iterator it_b = boost::range::lower_bound(s, 'b', iless);
    std::string::iterator it_c = boost::range::lower_bound(s, 'c', iless);

    std::cout << "Index of lower_bound(s, 'b', iless): " << (it_b - s.begin()) << std::endl;
    std::cout << "Index of lower_bound(s, 'c', iless): " << (it_c - s.begin()) << std::endl;
}

int main() {
    lower_bound_without_predicate();
    lower_bound_with_predicate();

    return 0;
}

Output:

Index of lower_bound(s, 'b'): 1
Index of lower_bound(s, 'c'): 1
Index of lower_bound(s, 'b', iless): 1
Index of lower_bound(s, 'c', iless): 1

 

Boost Range for Humans

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