[alg.min.max] (original) (raw)

26 Algorithms library [algorithms]

26.8.9 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,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<const T*, Proj>> Comp = ranges::less> constexpr const T& ranges::min(const T& a, const T& b, Comp comp = {}, Proj proj = {});

Returns: The smaller value.

Returns the first argument when the arguments are equivalent.

Complexity: Exactly one comparison and two applications of the projection, if any.

Remarks: An invocation may explicitly specify an argument for the template parameter Tof the overloads in namespace std.

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](concepts.object#concept:copyable "18.6 Object concepts [concepts.object]") T, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<const T*, Proj>> Comp = ranges::less> constexpr T ranges::min(initializer_list<T> r, Comp comp = {}, Proj proj = {});template<[input_range](range.refinements#concept:input%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> requires [indirectly_copyable_storable](alg.req.ind.copy#concept:indirectly%5Fcopyable%5Fstorable "24.3.7.3 Concept indirectly_­copyable [alg.req.ind.copy]")<iterator_t<R>, range_value_t<R>*> constexpr range_value_t<R> ranges::min(R&& r, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [_sized-random-access-range_](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> requires [indirectly_copyable_storable](alg.req.ind.copy#concept:indirectly%5Fcopyable%5Fstorable "24.3.7.3 Concept indirectly_­copyable [alg.req.ind.copy]")<iterator_t<R>, range_value_t<R>*> range_value_t<R> ranges::min(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {});

Preconditions: ranges​::​distance(r) > 0.

Returns: The smallest value in the input range.

Returns a copy of the leftmost element when several elements are equivalent to the smallest.

Complexity: Exactly ranges​::​distance(r) - 1 comparisons and twice as many applications of the projection, if any.

Remarks: An invocation may explicitly specify an argument for the template parameter Tof the overloads in namespace std.

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,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<const T*, Proj>> Comp = ranges::less> constexpr const T& ranges::max(const T& a, const T& b, Comp comp = {}, Proj proj = {});

Returns: The larger value.

Returns the first argument when the arguments are equivalent.

Complexity: Exactly one comparison and two applications of the projection, if any.

Remarks: An invocation may explicitly specify an argument for the template parameter Tof the overloads in namespace std.

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](concepts.object#concept:copyable "18.6 Object concepts [concepts.object]") T, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<const T*, Proj>> Comp = ranges::less> constexpr T ranges::max(initializer_list<T> r, Comp comp = {}, Proj proj = {});template<[input_range](range.refinements#concept:input%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> requires [indirectly_copyable_storable](alg.req.ind.copy#concept:indirectly%5Fcopyable%5Fstorable "24.3.7.3 Concept indirectly_­copyable [alg.req.ind.copy]")<iterator_t<R>, range_value_t<R>*> constexpr range_value_t<R> ranges::max(R&& r, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [_sized-random-access-range_](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> requires [indirectly_copyable_storable](alg.req.ind.copy#concept:indirectly%5Fcopyable%5Fstorable "24.3.7.3 Concept indirectly_­copyable [alg.req.ind.copy]")<iterator_t<R>, range_value_t<R>*> range_value_t<R> ranges::max(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {});

Preconditions: ranges​::​distance(r) > 0.

Returns: The largest value in the input range.

Returns a copy of the leftmost element when several elements are equivalent to the largest.

Complexity: Exactly ranges​::​distance(r) - 1 comparisons and twice as many applications of the projection, if any.

Remarks: An invocation may explicitly specify an argument for the template parameter Tof the overloads in namespace std.

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,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<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 = {});

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

Complexity: Exactly one comparison and two applications of the projection, if any.

Remarks: An invocation may explicitly specify an argument for the template parameter Tof the overloads in namespace std.

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](concepts.object#concept:copyable "18.6 Object concepts [concepts.object]") T, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<const T*, Proj>> Comp = ranges::less> constexpr ranges::minmax_result<T> ranges::minmax(initializer_list<T> r, Comp comp = {}, Proj proj = {});template<[input_range](range.refinements#concept:input%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> requires [indirectly_copyable_storable](alg.req.ind.copy#concept:indirectly%5Fcopyable%5Fstorable "24.3.7.3 Concept indirectly_­copyable [alg.req.ind.copy]")<iterator_t<R>, range_value_t<R>*> constexpr ranges::minmax_result<range_value_t<R>> ranges::minmax(R&& r, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [_sized-random-access-range_](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> requires [indirectly_copyable_storable](alg.req.ind.copy#concept:indirectly%5Fcopyable%5Fstorable "24.3.7.3 Concept indirectly_­copyable [alg.req.ind.copy]")<iterator_t<R>, range_value_t<R>*> ranges::minmax_result<range_value_t<R>> ranges::minmax(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {});

Preconditions: ranges​::​distance(r) > 0.

Returns: Let X be the return type.

Returns X{x, y}, where x is a copy of the leftmost element with the smallest value andy a copy of the rightmost element with the largest value in the input range.

Complexity: At most applications of the corresponding predicate and twice as many applications of the projection, if any.

Remarks: An invocation may explicitly specify an argument for the template parameter Tof the overloads in namespace std.

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<[forward_iterator](iterator.concept.forward#concept:forward%5Fiterator "24.3.4.11 Concept forward_­iterator [iterator.concept.forward]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> constexpr I ranges::min_element(I first, S last, Comp comp = {}, Proj proj = {});template<[forward_range](range.refinements#concept:forward%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> constexpr borrowed_iterator_t<R> ranges::min_element(R&& r, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<I> S,class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> I ranges::min_element(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [_sized-random-access-range_](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R,class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> borrowed_iterator_t<R> ranges::min_element(Ep&& exec, 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<[forward_iterator](iterator.concept.forward#concept:forward%5Fiterator "24.3.4.11 Concept forward_­iterator [iterator.concept.forward]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> constexpr I ranges::max_element(I first, S last, Comp comp = {}, Proj proj = {});template<[forward_range](range.refinements#concept:forward%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> constexpr borrowed_iterator_t<R> ranges::max_element(R&& r, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<I> S,class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> I ranges::max_element(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [_sized-random-access-range_](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R,class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> borrowed_iterator_t<R> ranges::max_element(Ep&& exec, 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<[forward_iterator](iterator.concept.forward#concept:forward%5Fiterator "24.3.4.11 Concept forward_­iterator [iterator.concept.forward]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> constexpr ranges::minmax_element_result<I> ranges::minmax_element(I first, S last, Comp comp = {}, Proj proj = {});template<[forward_range](range.refinements#concept:forward%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> constexpr ranges::minmax_element_result<borrowed_iterator_t<R>> ranges::minmax_element(R&& r, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<I> S,class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> ranges::minmax_element_result<I> ranges::minmax_element(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [_sized-random-access-range_](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> ranges::minmax_element_result<borrowed_iterator_t<R>> ranges::minmax_element(Ep&& exec, 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 iterator204in [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.