std::lower_bound
std::lower_bound returns an iterator to the first element in a sorted range that is not less than a value. If every element is less, it returns end. That iterator is the insertion point that keeps the range sorted.
Detailed Explanation
Think of a sorted vector as a number line of elements. lower_bound(v, x) walks (by binary search) to the leftmost position where you could put x without breaking the order. If x is already there several times, you get the first of them. If x is larger than every element, you get end().
upper_bound(v, x) is the first element greater than x — the position just past the last equivalent element. equal_range returns {lower_bound, upper_bound}: the slice of values equivalent to x.
These three are the real binary-search API. binary_search is lower_bound plus a check that the iterator is not end and the element is equivalent.
On a random-access range the cost is O(log n) comparisons. On a bidirectional range it is O(n) iterator increments plus O(log n) comparisons. map::lower_bound and set::lower_bound are members and are O(log n) on the tree; prefer them over std::lower_bound on those containers (the free function would walk).
Insertion: v.insert(std::lower_bound(v.begin(), v.end(), x), x); keeps a sorted vector sorted. That insert is O(n) because of the shift. If you insert often, a set is the better structure. If you build once and query many times, a sorted vector plus lower_bound is the faster structure.
Predecessor queries: the element strictly less than x is --std::lower_bound(...) when that is not begin. Be careful at begin. Successor is lower_bound itself if *it != x, or upper_bound if you want strictly greater.
The range must be partitioned with respect to the comparator: every element before the bound is less, every element after is not. A fully sorted range satisfies that. A range that is only partitioned (like after std::partition) is enough for a related algorithm (partition_point) but not for lower_bound's documented requirement of a sorted range.
C++20 projections: ranges::lower_bound(users, id, {}, &User::id) if users is sorted by id.
Code Examples
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{1, 3, 3, 3, 9};
auto lo = std::lower_bound(v.begin(), v.end(), 3);
auto hi = std::upper_bound(v.begin(), v.end(), 3);
std::cout << (lo - v.begin()) << ' ' << (hi - v.begin()) << '\n';
// prints: 1 4 (the three 3s occupy [1, 4))
}#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{1, 4, 9};
int x = 5;
auto it = std::lower_bound(v.begin(), v.end(), x);
v.insert(it, x);
for (int n : v) std::cout << n << ' ';
std::cout << '\n'; // prints: 1 4 5 9
}Common Gotchas
Using the iterator without checking `end`
if `x` is larger than every element, `*lower_bound` is undefined.
Equivalent elements, not `==`
with a custom comparator, `lower_bound` for `x` can land on an element that is `==` in the comparator sense only.
Inserting via `lower_bound` on a vector is O(n)
the search is log n; the shift is the cost.