cpp/algorithm/upper bound

Returns an iterator pointing to the first element in the range such that  (or ) is  (i.e. strictly greater), or  if no such element is found.

The range must be partitioned with respect to the expression  or, i.e., all elements for which the expression is  must precede all elements for which the expression is. A fully-sorted range meets this criterion.

The first version uses to compare the elements, the second version uses the given comparison function.

Return value
Iterator pointing to the first element in the range such that  (or ) is, or  if no such element is found.

Complexity
The number of comparisons performed is logarithmic in the distance between and  (At most $log 2(last - first) + O(1)$ comparisons).

However, for non-s, the number of iterator increments is linear. Notably, std, std, std, and std iterators are not random access, and so their member functions should be preferred.

Possible implementation
See also the implementations in libstdc++ and libc++.