std::binary_search
std::binary_search reports whether a sorted range contains a value. It is O(log n) on random-access iterators. It returns a bool, not a position; use lower_bound when you need the iterator.
Detailed Explanation
Binary search halves a sorted range: compare the middle, then look left or right. After about log₂ n steps you know whether the value is present. The range must already be sorted by the same comparator you pass in. An unsorted range does not crash; it returns a wrong answer.
std::binary_search(first, last, value) is true if there is an element equivalent to value. Equivalence is !comp(x, value) && !comp(value, x), not necessarily ==. A comparator that only looks at a field makes two structs with different other fields equivalent.
It does not give you the element. On a miss you cannot see where the value would be inserted. lower_bound returns the first iterator not less than the value, which is the insert point and, if *it == value, the element. Almost every time you reach for binary_search, you wanted lower_bound.
On bidirectional iterators (list) the algorithms still work and become O(n) because advancing to the middle is a walk. The log n claim assumes random access. Sorting a list and binary-searching it is therefore a waste; use set or copy to a vector.
Hand-rolled binary search is a common source of overflow: (lo + hi) / 2 overflows signed int when both are large. Write lo + (hi - lo) / 2. The standard algorithms already get this right.
C++20 std::ranges::binary_search(v, x) and a projection: ranges::binary_search(users, 3, {}, &User::id) requires the range to be sorted by id.
When n is tiny, a linear find can be faster (simpler inner loop, no branchy mid-point). Binary search wins when n is large and you search more than once (so the sort pays for itself), or when the data is already sorted.
equal_range is a pair of lower_bound and upper_bound: the half-open slice of equivalent elements. Use it for "all rows with this key" in a sorted vector of pairs.
Code Examples
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{1, 3, 5, 7, 9};
std::cout << std::boolalpha
<< std::binary_search(v.begin(), v.end(), 7) << '\n'; // true
std::cout << std::binary_search(v.begin(), v.end(), 6) << '\n'; // false
}#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{1, 3, 5, 7, 9};
auto it = std::lower_bound(v.begin(), v.end(), 6);
std::cout << (it - v.begin()) << '\n'; // prints: 3 (insert 6 before 7)
}Common Gotchas
Searching an unsorted range
the result is meaningless. Sort first, or use `find`.
`binary_search` is a `bool`
you still have to `lower_bound` to get the index. Do not call both; call `lower_bound` and test `it != end && *it == value`.
Comparator must match the sort
sorting with `greater` and searching with `less` is wrong. Pass the same comparator.
On a `list`, it is linear
the algorithm is still "binary" in comparisons but each mid-step walks. Prefer a `set` or a `vector`.