Share via


binary_search

binary_search

template<class FwdIt, class T>
    bool binary_search(FwdIt first, FwdIt last, const T& val);
template<class FwdIt, class T, class Pred>
    bool binary_search(FwdIt first, FwdIt last, const T& val,
        Pred pr);

The first template function determines whether a value of N exists in the range [0, last - first) for which *(first + N) has equivalent ordering to val, where the elements designated by iterators in the range [first, last) form a sequence ordered byoperator<. If so, the function returns true. If no such value exists, it returns false.

If FwdIt is a random-access iterator type, the function evaluates the ordering predicate X < Y at most ceil(log(last - first)) + 2 times. Otherwise, the function evaluates the predicate a number of times proportional to last - first.

The second template function behaves the same, except that it replaces operator<(X, Y) with pr(X, Y).