# 11 Algorithms library [algorithms]

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

### 11.5.7 Minimum and maximum [alg.min.max]

 template <class T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = less<>> constexpr const T& min(const T& a, const T& b, Comp comp = Comp{}, Proj proj = Proj{}); 

Returns: The smaller value.

Remarks: Returns the first argument when the arguments are equivalent.

 template <Copyable T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = less<>> constexpr T min(initializer_list<T> rng, Comp comp = Comp{}, Proj proj = Proj{}); template <InputRange Rng, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<Rng>, Proj>> Comp = less<>> requires Copyable<value_type_t<iterator_t<Rng>>> value_type_t<iterator_t<Rng>> min(Rng&& rng, Comp comp = Comp{}, Proj proj = Proj{}); 

Requires: distance(rng) > 0.

Returns: The smallest value in the initializer_list or range.

Remarks: Returns a copy of the leftmost argument when several arguments are equivalent to the smallest.

 template <class T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = less<>> constexpr const T& max(const T& a, const T& b, Comp comp = Comp{}, Proj proj = Proj{}); 

Returns: The larger value.

Remarks: Returns the first argument when the arguments are equivalent.

 template <Copyable T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = less<>> constexpr T max(initializer_list<T> rng, Comp comp = Comp{}, Proj proj = Proj{}); template <InputRange Rng, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<Rng>, Proj>> Comp = less<>> requires Copyable<value_type_t<iterator_t<Rng>>> value_type_t<iterator_t<Rng>> max(Rng&& rng, Comp comp = Comp{}, Proj proj = Proj{}); 

Requires: distance(rng) > 0.

Returns: The largest value in the initializer_list or range.

Remarks: Returns a copy of the leftmost argument when several arguments are equivalent to the largest.

 template <class T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = less<>> constexpr tagged_pair<tag::min(const T&), tag::max(const T&)> minmax(const T& a, const T& b, Comp comp = Comp{}, Proj proj = Proj{}); 

Returns: {b, a} if b is smaller than a, and {a, b} otherwise.

Remarks: Returns {a, b} when the arguments are equivalent.

Complexity: Exactly one comparison and exactly two applications of the projection.

 template <Copyable T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = less<>> constexpr tagged_pair<tag::min(T), tag::max(T)> minmax(initializer_list<T> rng, Comp comp = Comp{}, Proj proj = Proj{}); template <InputRange Rng, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<Rng>, Proj> Comp = less<>> requires Copyable<value_type_t<iterator_t<Rng>>> tagged_pair<tag::min(value_type_t<iterator_t<Rng>>), tag::max(value_type_t<iterator_t<Rng>>)> minmax(Rng&& rng, Comp comp = Comp{}, Proj proj = Proj{}); 

Requires: distance(rng) > 0.

Returns: {x, y}, where x has the smallest and y has the largest value in the initializer_list or range.

Remarks: x is a copy of the leftmost argument when several arguments are equivalent to the smallest. y is a copy of the rightmost argument when several arguments are equivalent to the largest.

Complexity: At most (3/2) * distance(rng) applications of the corresponding predicate, and at most twice as many applications of the projection.

 template <ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = less<>> I min_element(I first, S last, Comp comp = Comp{}, Proj proj = Proj{}); template <ForwardRange Rng, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<Rng>, Proj>> Comp = less<>> safe_iterator_t<Rng> min_element(Rng&& rng, Comp comp = Comp{}, Proj proj = Proj{}); 

Returns: The first iterator i in the range [first,last) such that for every iterator j in the range [first,last) the following corresponding condition holds:
invoke(comp, invoke(proj, *j), invoke(proj, *i)) == false. Returns last if first == last.

Complexity: Exactly max((last - first) - 1, 0) applications of the comparison function and exactly twice as many applications of the projection.

 template <ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = less<>> I max_element(I first, S last, Comp comp = Comp{}, Proj proj = Proj{}); template <ForwardRange Rng, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<Rng>, Proj>> Comp = less<>> safe_iterator_t<Rng> max_element(Rng&& rng, Comp comp = Comp{}, Proj proj = Proj{}); 

Returns: The first iterator i in the range [first,last) such that for every iterator j in the range [first,last) the following corresponding condition holds:
invoke(comp, invoke(proj, *i), invoke(proj, *j)) == false. Returns last if first == last.

Complexity: Exactly max((last - first) - 1, 0) applications of the comparison function and exactly twice as many applications of the projection.

 template <ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = less<>> tagged_pair<tag::min(I), tag::max(I)> minmax_element(I first, S last, Comp comp = Comp{}, Proj proj = Proj{}); template <ForwardRange Rng, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<Rng>, Proj>> Comp = less<>> tagged_pair<tag::min(safe_iterator_t<Rng>), tag::max(safe_iterator_t<Rng>)> minmax_element(Rng&& rng, Comp comp = Comp{}, Proj proj = Proj{}); 

Returns: {first, first} if [first,last) is empty, otherwise {m, M}, where m is the first iterator in [first,last) such that no iterator in the range refers to a smaller element, and where M is the last iterator in [first,last) such that no iterator in the range refers to a larger element.

Complexity: At most applications of the comparison function and at most twice as many applications of the projection, where N is distance(first, last).