# 11 Algorithms library [algorithms]

## 11.5 Sorting and related operations [alg.sorting]

### 11.5.3 Binary search [alg.binary.search]

#### 11.5.3.4binary_search[binary.search]

``` template <ForwardIterator I, Sentinel<I> S, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<I, Proj>> Comp = less<>> bool binary_search(I first, S last, const T& value, Comp comp = Comp{}, Proj proj = Proj{}); template <ForwardRange Rng, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<iterator_t<Rng>, Proj>> Comp = less<>> bool binary_search(Rng&& rng, const T& value, Comp comp = Comp{}, Proj proj = Proj{}); ```

Requires: The elements e of [first,last) are partitioned with respect to the expressions invoke(comp, invoke(proj, e), value) and !invoke(comp, value, invoke(proj, e)). Also, for all elements e of [first, last), invoke(comp, invoke(proj, e), value) shall imply !invoke(comp, value, invoke(proj, e)).

Returns: true if there is an iterator i in the range [first,last) that satisfies the corresponding conditions: invoke(comp, invoke(proj, *i), value) == false && invoke(comp, value, invoke(proj, *i)) == false.

Complexity: At most log2(last - first) + Ο(1) applications of the comparison function and projection.