25 Algorithms library [algorithms]

25.7 Sorting and related operations [alg.sorting]

25.7.8 Minimum and maximum [alg.min.max]

template<class T> constexpr const T& min(const T& a, const T& b); template<class T, class Compare> constexpr const T& min(const T& a, const T& b, Compare comp); template<class T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less> constexpr const T& ranges::min(const T& a, const T& b, Comp comp = {}, Proj proj = {});
Requires: For the first form, type T shall be Cpp17LessThanComparable (Table 24).
Returns: The smaller value.
Remarks: Returns the first argument when the arguments are equivalent.
An invocation may explicitly specify an argument for the template parameter T of the overloads in namespace std.
Complexity: Exactly one comparison and two applications of the projection, if any.
template<class T> constexpr T min(initializer_list<T> r); template<class T, class Compare> constexpr T min(initializer_list<T> r, Compare comp); template<Copyable T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less> constexpr T ranges::min(initializer_list<T> r, Comp comp = {}, Proj proj = {}); template<InputRange R, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<R>, Proj>> Comp = ranges::less> requires IndirectlyCopyableStorable<iterator_t<R>, iter_value_t<iterator_t<R>>*> constexpr iter_value_t<iterator_t<R>> ranges::min(R&& r, Comp comp = {}, Proj proj = {});
Requires: ranges::distance(r) > 0.
For the overloads in namespace std, T shall be Cpp17CopyConstructible.
For the first form, type T shall be Cpp17LessThanComparable.
Returns: The smallest value in the input range.
Remarks: Returns a copy of the leftmost element when several elements are equivalent to the smallest.
An invocation may explicitly specify an argument for the template parameter T of the overloads in namespace std.
Complexity: Exactly ranges::distance(r) - 1 comparisons and twice as many applications of the projection, if any.
template<class T> constexpr const T& max(const T& a, const T& b); template<class T, class Compare> constexpr const T& max(const T& a, const T& b, Compare comp); template<class T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less> constexpr const T& ranges::max(const T& a, const T& b, Comp comp = {}, Proj proj = {});
Requires: For the first form, type T shall be Cpp17LessThanComparable (Table 24).
Returns: The larger value.
Remarks: Returns the first argument when the arguments are equivalent.
An invocation may explicitly specify an argument for the template parameter T of the overloads in namespace std.
Complexity: Exactly one comparison and two applications of the projection, if any.
template<class T> constexpr T max(initializer_list<T> r); template<class T, class Compare> constexpr T max(initializer_list<T> r, Compare comp); template<Copyable T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less> constexpr T ranges::max(initializer_list<T> r, Comp comp = {}, Proj proj = {}); template<InputRange R, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<R>, Proj>> Comp = ranges::less> requires IndirectlyCopyableStorable<iterator_t<R>, iter_value_t<iterator_t<R>>*> constexpr iter_value_t<iterator_t<R>> ranges::max(R&& r, Comp comp = {}, Proj proj = {});
Requires: ranges::distance(r) > 0.
For the overloads in namespace std, T shall be Cpp17CopyConstructible.
For the first form, type T shall be Cpp17LessThanComparable.
Returns: The largest value in the input range.
Remarks: Returns a copy of the leftmost element when several elements are equivalent to the largest.
An invocation may explicitly specify an argument for the template parameter T of the overloads in namespace std.
Complexity: Exactly ranges::distance(r) - 1 comparisons and twice as many applications of the projection, if any.
template<class T> constexpr pair<const T&, const T&> minmax(const T& a, const T& b); template<class T, class Compare> constexpr pair<const T&, const T&> minmax(const T& a, const T& b, Compare comp); template<class T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less> constexpr ranges::minmax_result<const T&> ranges::minmax(const T& a, const T& b, Comp comp = {}, Proj proj = {});
Requires: For the first form, type T shall be Cpp17LessThanComparable (Table 24).
Returns: {b, a} if b is smaller than a, and {a, b} otherwise.
Remarks: An invocation may explicitly specify an argument for the template parameter T of the overloads in namespace std.
Complexity: Exactly one comparison and two applications of the projection, if any.
template<class T> constexpr pair<T, T> minmax(initializer_list<T> t); template<class T, class Compare> constexpr pair<T, T> minmax(initializer_list<T> t, Compare comp); template<Copyable T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less> constexpr ranges::minmax_result<T> ranges::minmax(initializer_list<T> r, Comp comp = {}, Proj proj = {}); template<InputRange R, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<R>, Proj>> Comp = ranges::less> requires IndirectlyCopyableStorable<iterator_t<R>, iter_value_t<iterator_t<R>>*> constexpr ranges::minmax_result<iter_value_t<iterator_t<R>>> ranges::minmax(R&& r, Comp comp = {}, Proj proj = {});
Requires: ranges::distance(r) > 0.
For the overloads in namespace std, T shall be Cpp17CopyConstructible.
For the first form, type T shall be Cpp17LessThanComparable.
Returns: Let X be the return type.
Returns Xx, y, where x is a copy of the leftmost element with the smallest and y a copy of the rightmost element with the largest value in the input range.
Remarks: An invocation may explicitly specify an argument for the template parameter T of the overloads in namespace std.
Complexity: At most applications of the corresponding predicate and twice as many applications of the projection, if any.
template<class ForwardIterator> constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator min_element(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last); template<class ForwardIterator, class Compare> constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last, Compare comp); template<class ExecutionPolicy, class ForwardIterator, class Compare> ForwardIterator min_element(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Compare comp); template<ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less> constexpr I ranges::min_element(I first, S last, Comp comp = {}, Proj proj = {}); template<ForwardRange R, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<R>, Proj>> Comp = ranges::less> constexpr safe_iterator_t<R> ranges::min_element(R&& r, Comp comp = {}, Proj proj = {});
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Returns: The first iterator i in the range [first, last) such that for every iterator j in the range [first, last),
bool(invoke(comp, invoke(proj, *j), invoke(proj, *i)))
is false.
Returns last if first == last.
Complexity: Exactly comparisons and twice as many projections.
template<class ForwardIterator> constexpr ForwardIterator max_element(ForwardIterator first, ForwardIterator last); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator max_element(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last); template<class ForwardIterator, class Compare> constexpr ForwardIterator max_element(ForwardIterator first, ForwardIterator last, Compare comp); template<class ExecutionPolicy, class ForwardIterator, class Compare> ForwardIterator max_element(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Compare comp); template<ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less> constexpr I ranges::max_element(I first, S last, Comp comp = {}, Proj proj = {}); template<ForwardRange R, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<R>, Proj>> Comp = ranges::less> constexpr safe_iterator_t<R> ranges::max_element(R&& r, Comp comp = {}, Proj proj = {});
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Returns: The first iterator i in the range [first, last) such that for every iterator j in the range [first, last),
bool(invoke(comp, invoke(proj, *i), invoke(proj, *j)))
is false.
Returns last if first == last.
Complexity: Exactly comparisons and twice as many projections.
template<class ForwardIterator> constexpr pair<ForwardIterator, ForwardIterator> minmax_element(ForwardIterator first, ForwardIterator last); template<class ExecutionPolicy, class ForwardIterator> pair<ForwardIterator, ForwardIterator> minmax_element(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last); template<class ForwardIterator, class Compare> constexpr pair<ForwardIterator, ForwardIterator> minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); template<class ExecutionPolicy, class ForwardIterator, class Compare> pair<ForwardIterator, ForwardIterator> minmax_element(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Compare comp); template<ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less> constexpr ranges::minmax_result<I> ranges::minmax_element(I first, S last, Comp comp = {}, Proj proj = {}); template<ForwardRange R, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<R>, Proj>> Comp = ranges::less> constexpr ranges::minmax_result<safe_iterator_t<R>> ranges::minmax_element(R&& r, Comp comp = {}, 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 iterator238 in [first, last) such that no iterator in the range refers to a larger element.
Complexity: Let N be last - first.
At most comparisons and twice as many applications of the projection, if any.
This behavior intentionally differs from max_­element.