24 Algorithms library [algorithms]

24.7 Sorting and related operations [alg.sorting]

The operations in [alg.sorting] defined directly in namespace std have two versions: one that takes a function object of type Compare and one that uses an operator<.
Compare is a function object type ([function.objects]) that meets the requirements for a template parameter named BinaryPredicate ([algorithms.requirements]).
The return value of the function call operation applied to an object of type Compare, when contextually converted to bool ([conv]), yields true if the first argument of the call is less than the second, and false otherwise.
Compare comp is used throughout for algorithms assuming an ordering relation.
For all algorithms that take Compare, there is a version that uses operator< instead.
That is, comp(*i, *j) != false defaults to *i < *j != false.
For algorithms other than those described in [alg.binary.search], comp shall induce a strict weak ordering on the values.
The term strict refers to the requirement of an irreflexive relation (!comp(x, x) for all x), and the term weak to requirements that are not as strong as those for a total ordering, but stronger than those for a partial ordering.
If we define equiv(a, b) as !comp(a, b) && !comp(b, a), then the requirements are that comp and equiv both be transitive relations:
  • comp(a, b) && comp(b, c) implies comp(a, c)
  • equiv(a, b) && equiv(b, c) implies equiv(a, c)
[Note
:
Under these conditions, it can be shown that
  • equiv is an equivalence relation,
  • comp induces a well-defined relation on the equivalence classes determined by equiv, and
  • the induced relation is a strict total ordering.
end note
]
A sequence is sorted with respect to a comp and proj for a comparator and projection comp and proj if for every iterator i pointing to the sequence and every non-negative integer n such that i + n is a valid iterator pointing to an element of the sequence,
bool(invoke(comp, invoke(proj, *(i + n)), invoke(proj, *i)))
is false.
A sequence [start, finish) is partitioned with respect to an expression f(e) if there exists an integer n such that for all 0 <= i < (finish - start), f(*(start + i)) is true if and only if i < n.
In the descriptions of the functions that deal with ordering relationships we frequently use a notion of equivalence to describe concepts such as stability.
The equivalence to which we refer is not necessarily an operator==, but an equivalence relation induced by the strict weak ordering.
That is, two elements a and b are considered equivalent if and only if !(a < b) && !(b < a).

24.7.1 Sorting [alg.sort]

24.7.1.1 sort [sort]

template<class RandomAccessIterator> constexpr void sort(RandomAccessIterator first, RandomAccessIterator last); template<class ExecutionPolicy, class RandomAccessIterator> void sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> constexpr void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); template<class ExecutionPolicy, class RandomAccessIterator, class Compare> void sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last, Compare comp); namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr I sort(I first, S last, Comp comp = {}, Proj proj = {}); template<RandomAccessRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr safe_iterator_t<R> sort(R&& r, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Requires: For the overloads in namespace std, RandomAccessIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: Sorts the elements in the range [first, last) with respect to comp and proj.
Returns: last, for the overloads in namespace ranges.
Complexity: Let N be last - first.
comparisons and projections.

24.7.1.2 stable_­sort [stable.sort]

template<class RandomAccessIterator> void stable_sort(RandomAccessIterator first, RandomAccessIterator last); template<class ExecutionPolicy, class RandomAccessIterator> void stable_sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> void stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); template<class ExecutionPolicy, class RandomAccessIterator, class Compare> void stable_sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last, Compare comp); namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> I stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); template<RandomAccessRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> safe_iterator_t<R> stable_sort(R&& r, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Requires: For the overloads in namespace std, RandomAccessIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: Sorts the elements in the range [first, last) with respect to comp and proj.
Returns: last for the overloads in namespace ranges.
Complexity: Let N be last - first.
If enough extra memory is available, comparisons.
Otherwise, at most comparisons.
In either case, twice as many projections as the number of comparisons.
Remarks: Stable ([algorithm.stable]).

24.7.1.3 partial_­sort [partial.sort]

template<class RandomAccessIterator> constexpr void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); template<class ExecutionPolicy, class RandomAccessIterator> void partial_sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> constexpr void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); template<class ExecutionPolicy, class RandomAccessIterator, class Compare> void partial_sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr I partial_sort(I first, I middle, S last, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Requires: [first, middle) and [middle, last) shall be valid ranges.
For the overloads in namespace std, RandomAccessIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: Places the first middle - first elements from the range [first, last) as sorted with respect to comp and proj into the range [first, middle).
The rest of the elements in the range [middle, last) are placed in an unspecified order.
Returns: last for the overload in namespace ranges.
Complexity: Approximately (last - first) * log(middle - first) comparisons, and twice as many projections.
namespace ranges { template<RandomAccessRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr safe_iterator_t<R> partial_sort(R&& r, iterator_t<R> middle, Comp comp = {}, Proj proj = {}); }
Effects: Equivalent to:
return ranges::partial_sort(ranges::begin(r), middle, ranges::end(r), comp, proj);

24.7.1.4 partial_­sort_­copy [partial.sort.copy]

template<class InputIterator, class RandomAccessIterator> constexpr RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last); template<class ExecutionPolicy, class ForwardIterator, class RandomAccessIterator> RandomAccessIterator partial_sort_copy(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last); template<class InputIterator, class RandomAccessIterator, class Compare> constexpr RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); template<class ExecutionPolicy, class ForwardIterator, class RandomAccessIterator, class Compare> RandomAccessIterator partial_sort_copy(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); namespace ranges { template<InputIterator I1, Sentinel<I1> S1, RandomAccessIterator I2, Sentinel<I2> S2, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires IndirectlyCopyable<I1, I2> && Sortable<I2, Comp, Proj2> && IndirectStrictWeakOrder<Comp, projected<I1, Proj1>, projected<I2, Proj2>> constexpr I2 partial_sort_copy(I1 first, S1 last, I2 result_first, S2 result_last, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<InputRange R1, RandomAccessRange R2, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires IndirectlyCopyable<iterator_t<R1>, iterator_t<R2>> && Sortable<iterator_t<R2>, Comp, Proj2> && IndirectStrictWeakOrder<Comp, projected<iterator_t<R1>, Proj1>, projected<iterator_t<R2>, Proj2>> constexpr safe_iterator_t<R2> partial_sort_copy(R1&& r, R2&& result_r, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); }
Let N be .
Let comp be less{}, and proj1 and proj2 be identity{} for the overloads with no parameters by those names.
Requires: For the overloads in namespace std, RandomAccessIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]), the type of *result_­first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements, and the expression *first shall be writable ([iterator.requirements.general]) to result_­first.
Expects: For iterators a1 and b1 in [first, last), and iterators x2 and y2 in [result_­first, result_­last), after evaluating the assignment *y2 = *b1, let E be the value of
bool(invoke(comp, invoke(proj1, *a1), invoke(proj2, *y2))).
Then, after evaluating the assignment *x2 = *a1, E is equal to
bool(invoke(comp, invoke(proj2, *x2), invoke(proj2, *y2))).
[Note
:
Writing a value from the input range into the output range does not affect how it is ordered by comp and proj1 or proj2.
end note
]
Effects: Places the first N elements as sorted with respect to comp and proj2 into the range [result_­first, result_­first + N).
Returns: result_­first + N.
Complexity: Approximately (last - first) * log N comparisons, and twice as many projections.

24.7.1.5 is_­sorted [is.sorted]

template<class ForwardIterator> constexpr bool is_sorted(ForwardIterator first, ForwardIterator last);
Effects: Equivalent to: return is_­sorted_­until(first, last) == last;
template<class ExecutionPolicy, class ForwardIterator> bool is_sorted(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last);
Effects: Equivalent to:
return is_sorted_until(std::forward<ExecutionPolicy>(exec), first, last) == last;
template<class ForwardIterator, class Compare> constexpr bool is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
Effects: Equivalent to: return is_­sorted_­until(first, last, comp) == last;
template<class ExecutionPolicy, class ForwardIterator, class Compare> bool is_sorted(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Compare comp);
Effects: Equivalent to:
is_sorted_until(std::forward<ExecutionPolicy>(exec), first, last, comp) == last
namespace ranges { template<ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less<>> constexpr bool is_sorted(I first, S last, Comp comp = {}, Proj proj = {}); template<ForwardRange R, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<R>, Proj>> Comp = ranges::less<>> constexpr bool is_sorted(R&& r, Comp comp = {}, Proj proj = {}); }
Effects: Equivalent to: return ranges::is_­sorted_­until(first, last, comp, proj) == last;
template<class ForwardIterator> constexpr ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator is_sorted_until(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last); template<class ForwardIterator, class Compare> constexpr ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp); template<class ExecutionPolicy, class ForwardIterator, class Compare> ForwardIterator is_sorted_until(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Compare comp); namespace ranges { template<ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less<>> constexpr I is_sorted_until(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> is_sorted_until(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 last iterator i in [first, last] for which the range [first, i) is sorted with respect to comp and proj.
Complexity: Linear.

24.7.2 Nth element [alg.nth.element]

template<class RandomAccessIterator> constexpr void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); template<class ExecutionPolicy, class RandomAccessIterator> void nth_element(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> constexpr void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp); template<class ExecutionPolicy, class RandomAccessIterator, class Compare> void nth_element(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp); namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr I nth_element(I first, I nth, S last, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Requires: [first, nth) and [nth, last) shall be valid ranges.
For the overloads in namespace std, RandomAccessIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]), and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: After nth_­element the element in the position pointed to by nth is the element that would be in that position if the whole range were sorted with respect to comp and proj, unless nth == last.
Also for every iterator i in the range [first, nth) and every iterator j in the range [nth, last) it holds that: bool(invoke(comp, invoke(proj, *j), invoke(proj, *i))) is false.
Returns: last for the overload in namespace ranges.
Complexity: For the overloads with no ExecutionPolicy, linear on average.
For the overloads with an ExecutionPolicy, applications of the predicate, and swaps, where .
namespace ranges { template<RandomAccessRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr safe_iterator_t<R> nth_element(R&& r, iterator_t<R> nth, Comp comp = {}, Proj proj = {}); }
Effects: Equivalent to:
return ranges::nth_element(ranges::begin(r), nth, ranges::end(r), comp, proj);

24.7.3 Binary search [alg.binary.search]

All of the algorithms in this subclause are versions of binary search and assume that the sequence being searched is partitioned with respect to an expression formed by binding the search key to an argument of the comparison function.
They work on non-random access iterators minimizing the number of comparisons, which will be logarithmic for all types of iterators.
They are especially appropriate for random access iterators, because these algorithms do a logarithmic number of steps through the data structure.
For non-random access iterators they execute a linear number of steps.

24.7.3.1 lower_­bound [lower.bound]

template<class ForwardIterator, class T> constexpr ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& value); template<class ForwardIterator, class T, class Compare> constexpr ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); namespace ranges { template<ForwardIterator I, Sentinel<I> S, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<I, Proj>> Comp = ranges::less<>> constexpr I lower_bound(I first, S last, const T& value, Comp comp = {}, Proj proj = {}); template<ForwardRange R, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<iterator_t<R>, Proj>> Comp = ranges::less<>> constexpr safe_iterator_t<R> lower_bound(R&& r, const T& value, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
Requires: The elements e of [first, last) shall be partitioned with respect to the expression bool(invoke(comp, invoke(proj, e), value)).
Returns: The furthermost iterator i in the range [first, last] such that for every iterator j in the range [first, i), bool(invoke(comp, invoke(proj, *j), value)) is true.
Complexity: At most comparisons and projections.

24.7.3.2 upper_­bound [upper.bound]

template<class ForwardIterator, class T> constexpr ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T& value); template<class ForwardIterator, class T, class Compare> constexpr ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); namespace ranges { template<ForwardIterator I, Sentinel<I> S, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<I, Proj>> Comp = ranges::less<>> constexpr I upper_bound(I first, S last, const T& value, Comp comp = {}, Proj proj = {}); template<ForwardRange R, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<iterator_t<R>, Proj>> Comp = ranges::less<>> constexpr safe_iterator_t<R> upper_bound(R&& r, const T& value, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
Requires: The elements e of [first, last) shall be partitioned with respect to the expression !bool(invoke(comp, value, invoke(proj, e))).
Returns: The furthermost iterator i in the range [first, last] such that for every iterator j in the range [first, i), !bool(invoke(comp, value, invoke(proj, *j))) is true.
Complexity: At most comparisons and projections.

24.7.3.3 equal_­range [equal.range]

template<class ForwardIterator, class T> constexpr pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& value); template<class ForwardIterator, class T, class Compare> constexpr pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); namespace ranges { template<ForwardIterator I, Sentinel<I> S, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<I, Proj>> Comp = ranges::less<>> constexpr subrange<I> equal_range(I first, S last, const T& value, Comp comp = {}, Proj proj = {}); template<ForwardRange R, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<iterator_t<R>, Proj>> Comp = ranges::less<>> constexpr safe_subrange_t<R> equal_range(R&& r, const T& value, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
Requires: The elements e of [first, last) shall be partitioned with respect to the expressions bool(invoke(comp, invoke(proj, e), value)) and !bool(invoke(comp, value, invoke(proj, e))).
Also, for all elements e of [first, last), bool(comp(e, value)) shall imply !bool(comp(​value, e)) for the overloads in namespace std.
Returns:
  • For the overloads in namespace std:
    \{lower_bound(first, last, value, comp),
     upper_bound(first, last, value, comp)\}
    
  • For the overloads in namespace ranges: {ranges::lower_bound(first, last, value, comp, proj), ranges::upper_bound(first, last, value, comp, proj)}
Complexity: At most comparisons and projections.

24.7.3.4 binary_­search [binary.search]

template<class ForwardIterator, class T> constexpr bool binary_search(ForwardIterator first, ForwardIterator last, const T& value); template<class ForwardIterator, class T, class Compare> constexpr bool binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); namespace ranges { template<ForwardIterator I, Sentinel<I> S, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<I, Proj>> Comp = ranges::less<>> constexpr bool binary_search(I first, S last, const T& value, Comp comp = {}, Proj proj = {}); template<ForwardRange R, class T, class Proj = identity, IndirectStrictWeakOrder<const T*, projected<iterator_t<R>, Proj>> Comp = ranges::less<>> constexpr bool binary_search(R&& r, const T& value, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
Requires: The elements e of [first, last) shall be partitioned with respect to the expressions bool(invoke(comp, invoke(proj, e), value)) and !bool(invoke(comp, value, invoke(proj, e))).
Also, for all elements e of [first, last), bool(comp(e, value)) shall imply !bool(comp(​value, e)) for the overloads in namespace std.
Returns: true if and only if for some iterator i in the range [first, last), !bool(invoke(comp, invoke(proj, *i), value)) && !bool(invoke(comp, value, invoke(proj, *i))) is true.
Complexity: At most comparisons and projections.

24.7.4 Partitions [alg.partitions]

template<class InputIterator, class Predicate> constexpr bool is_partitioned(InputIterator first, InputIterator last, Predicate pred); template<class ExecutionPolicy, class ForwardIterator, class Predicate> bool is_partitioned(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Predicate pred); namespace ranges { template<InputIterator I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> constexpr bool is_partitioned(I first, S last, Pred pred, Proj proj = {}); template<InputRange R, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> constexpr bool is_partitioned(R&& r, Pred pred, Proj proj = {}); }
Let proj be identity{} for the overloads with no parameter named proj.
Returns: true if and only if the elements e of [first, last) are partitioned with respect to the expression bool(invoke(pred, invoke(proj, e))).
Complexity: Linear.
At most last - first applications of pred and proj.
template<class ForwardIterator, class Predicate> constexpr ForwardIterator partition(ForwardIterator first, ForwardIterator last, Predicate pred); template<class ExecutionPolicy, class ForwardIterator, class Predicate> ForwardIterator partition(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Predicate pred); namespace ranges { template<Permutable I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> constexpr I partition(I first, S last, Pred pred, Proj proj = {}); template<ForwardRange R, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> requires Permutable<iterator_t<R>> constexpr safe_iterator_t<R> partition(R&& r, Pred pred, Proj proj = {}); }
Let proj be identity{} for the overloads with no parameter named proj and let E(x) be bool(invoke(​pred, invoke(proj, x))).
Requires: For the overloads in namespace std, ForwardIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]).
Effects: Places all the elements e in [first, last) that satisfy E(e) before all the elements that do not.
Returns: An iterator i such that is true for every iterator j in [first, i) and false for every iterator j in [i, last).
Complexity: Let :
  • For the overload with no ExecutionPolicy, exactly N applications of the predicate and projection.
    At most swaps if the type of first meets the Cpp17BidirectionalIterator requirements for the overloads in namespace std or models BidirectionalIterator for the overloads in namespace ranges, and at most N swaps otherwise.
  • For the overload with an ExecutionPolicy, swaps and applications of the predicate.
template<class BidirectionalIterator, class Predicate> BidirectionalIterator stable_partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred); template<class ExecutionPolicy, class BidirectionalIterator, class Predicate> BidirectionalIterator stable_partition(ExecutionPolicy&& exec, BidirectionalIterator first, BidirectionalIterator last, Predicate pred); namespace ranges { template<BidirectionalIterator I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> requires Permutable<I> I stable_partition(I first, S last, Pred pred, Proj proj = {}); template<BidirectionalRange R, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> requires Permutable<iterator_t<R>> safe_iterator_t<R> stable_partition(R&& r, Pred pred, Proj proj = {}); }
Let proj be identity{} for the overloads with no parameter named proj and let E(x) be bool(invoke(​pred, invoke(proj, x))).
Requires: For the overloads in namespace std, BidirectionalIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: Places all the elements e in [first, last) that satisfy E(e) before all the elements that do not.
The relative order of the elements in both groups is preserved.
Returns: An iterator i such that for every iterator j in [first, i), is true, and for every iterator j in the range [i, last), is false,
Complexity: Let N = last - first:
  • For the overloads with no ExecutionPolicy, at most swaps, but only swaps if there is enough extra memory.
    Exactly N applications of the predicate and projection.
  • For the overload with an ExecutionPolicy, swaps and applications of the predicate.
template<class InputIterator, class OutputIterator1, class OutputIterator2, class Predicate> constexpr pair<OutputIterator1, OutputIterator2> partition_copy(InputIterator first, InputIterator last, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred); template<class ExecutionPolicy, class ForwardIterator, class ForwardIterator1, class ForwardIterator2, class Predicate> pair<ForwardIterator1, ForwardIterator2> partition_copy(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, ForwardIterator1 out_true, ForwardIterator2 out_false, Predicate pred); namespace ranges { template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O1, WeaklyIncrementable O2, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> requires IndirectlyCopyable<I, O1> && IndirectlyCopyable<I, O2> constexpr partition_copy_result<I, O1, O2> partition_copy(I first, S last, O1 out_true, O2 out_false, Pred pred, Proj proj = {}); template<InputRange R, WeaklyIncrementable O1, WeaklyIncrementable O2, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> requires IndirectlyCopyable<iterator_t<R>, O1> && IndirectlyCopyable<iterator_t<R>, O2> constexpr partition_copy_result<safe_iterator_t<R>, O1, O2> partition_copy(R&& r, O1 out_true, O2 out_false, Pred pred, Proj proj = {}); }
Let proj be identity{} for the overloads with no parameter named proj and let E(x) be bool(invoke(​pred, invoke(proj, x))).
Requires: The input range and output ranges shall not overlap.
For the overloads in namespace std, the expression *first shall be writable ([iterator.requirements.general]) to out_­true and out_­false.
[Note
:
For the overload with an ExecutionPolicy, there may be a performance cost if first's value type does not meet the Cpp17CopyConstructible requirements.
end note
]
Effects: For each iterator i in [first, last), copies *i to the output range beginning with out_­true if is true, or to the output range beginning with out_­false otherwise.
Returns: Let o1 be the end of the output range beginning at out_­true, and o2 the end of the output range beginning at out_­false.
Returns
  • {o1, o2} for the overloads in namespace std, or
  • {last, o1, o2} for the overloads in namespace ranges.
Complexity: Exactly last - first applications of pred and proj.
template<class ForwardIterator, class Predicate> constexpr ForwardIterator partition_point(ForwardIterator first, ForwardIterator last, Predicate pred); namespace ranges { template<ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> constexpr I partition_point(I first, S last, Pred pred, Proj proj = {}); template<ForwardRange R, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> constexpr safe_iterator_t<R> partition_point(R&& r, Pred pred, Proj proj = {}); }
Let proj be identity{} for the overloads with no parameter named proj and let E(x) be bool(invoke(​pred, invoke(proj, x))).
Requires: The elements e of [first, last) shall be partitioned with respect to E(e).
Returns: An iterator mid such that is true for all iterators i in [first, mid), and false for all iterators i in [mid, last)
Complexity: applications of pred and proj.

24.7.5 Merge [alg.merge]

template<class InputIterator1, class InputIterator2, class OutputIterator> constexpr OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator> ForwardIterator merge(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result); template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> constexpr OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator, class Compare> ForwardIterator merge(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result, Compare comp); namespace ranges { template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<I1, I2, O, Comp, Proj1, Proj2> constexpr merge_result<I1, I2, O> merge(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<InputRange R1, InputRange R2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2> constexpr merge_result<safe_iterator_t<R1>, safe_iterator_t<R2>, O> merge(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); }
Let N be (last1 - first1) + (last2 - first2).
Let comp be less{}, proj1 be identity{}, and proj2 be identity{}, for the overloads with no parameters by those names.
Requires: The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp and proj1 or proj2, respectively.
The resulting range shall not overlap with either of the original ranges.
Effects: Copies all the elements of the two ranges [first1, last1) and [first2, last2) into the range [result, result_­last), where result_­last is result + N.
If an element a precedes b in an input range, a is copied into the output range before b.
If e1 is an element of [first1, last1) and e2 of [first2, last2), e2 is copied into the output range before e1 if and only if bool(invoke(comp, invoke(proj2, e2), invoke(proj1, e1))) is true.
Returns:
  • result_­last for the overloads in namespace std, or
  • {last1, last2, result_­last} for the overloads in namespace ranges.
Complexity:
  • For the overloads with no ExecutionPolicy, at most comparisons and applications of each projection.
  • For the overloads with an ExecutionPolicy, comparisons.
Remarks: Stable ([algorithm.stable]).
template<class BidirectionalIterator> void inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last); template<class ExecutionPolicy, class BidirectionalIterator> void inplace_merge(ExecutionPolicy&& exec, BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last); template<class BidirectionalIterator, class Compare> void inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp); template<class ExecutionPolicy, class BidirectionalIterator, class Compare> void inplace_merge(ExecutionPolicy&& exec, BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp); namespace ranges { template<BidirectionalIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> I inplace_merge(I first, I middle, S last, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Requires: [first, middle) and [middle, last) shall be valid ranges sorted with respect to comp and proj.
For the overloads in namespace std, BidirectionalIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: Merges two sorted consecutive ranges [first, middle) and [middle, last), putting the result of the merge into the range [first, last).
The resulting range is sorted with respect to comp and proj.
Returns: last for the overload in namespace ranges.
Complexity: Let :
  • For the overloads with no ExecutionPolicy, and if enough additional memory is available, exactly comparisons.
  • Otherwise, comparisons.
In either case, twice as many projections as comparisons.
Remarks: Stable.
namespace ranges { template<BidirectionalRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> safe_iterator_t<R> inplace_merge(R&& r, iterator_t<R> middle, Comp comp = {}, Proj proj = {}); }
Effects: Equivalent to:
return ranges::inplace_merge(ranges::begin(r), middle, ranges::end(r), comp, proj);

24.7.6 Set operations on sorted structures [alg.set.operations]

This subclause defines all the basic set operations on sorted structures.
They also work with multisets ([multiset]) containing multiple copies of equivalent elements.
The semantics of the set operations are generalized to multisets in a standard way by defining set_­union() to contain the maximum number of occurrences of every element, set_­intersection() to contain the minimum, and so on.

24.7.6.1 includes [includes]

template<class InputIterator1, class InputIterator2> constexpr bool includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> bool includes(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template<class InputIterator1, class InputIterator2, class Compare> constexpr bool includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Compare> bool includes(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, Compare comp); namespace ranges { template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2, class Proj1 = identity, class Proj2 = identity, IndirectStrictWeakOrder<projected<I1, Proj1>, projected<I2, Proj2>> Comp = ranges::less<>> constexpr bool includes(I1 first1, S1 last1, I2 first2, S2 last2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<InputRange R1, InputRange R2, class Proj1 = identity, class Proj2 = identity, IndirectStrictWeakOrder<projected<iterator_t<R1>, Proj1>, projected<iterator_t<R2>, Proj2>> Comp = ranges::less<>> constexpr bool includes(R1&& r1, R2&& r2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); }
Let comp be less{}, proj1 be identity{}, and proj2 be identity{}, for the overloads with no parameters by those names.
Requires: The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp and proj1 or proj2, respectively.
Returns: true if and only if [first2, last2) is a subsequence of [first1, last1).
[Note
:
A sequence S is a subsequence of another sequence T if S can be obtained from T by removing some, all, or none of T's elements and keeping the remaining elements in the same order.
end note
]
Complexity: At most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons and applications of each projection.

24.7.6.2 set_­union [set.union]

template<class InputIterator1, class InputIterator2, class OutputIterator> constexpr OutputIterator set_union(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator> ForwardIterator set_union(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result); template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> constexpr OutputIterator set_union(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator, class Compare> ForwardIterator set_union(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result, Compare comp); namespace ranges { template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<I1, I2, O, Comp, Proj1, Proj2> constexpr set_union_result<I1, I2, O> set_union(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<InputRange R1, InputRange R2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2> constexpr set_union_result<safe_iterator_t<R1>, safe_iterator_t<R2>, O> set_union(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); }
Let comp be less{}, and proj1 and proj2 be identity{} for the overloads with no parameters by those names.
Requires: The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp and proj1 or proj2, respectively.
The resulting range shall not overlap with either of the original ranges.
Effects: Constructs a sorted union of the elements from the two ranges; that is, the set of elements that are present in one or both of the ranges.
Returns: Let result_­last be the end of the constructed range.
Returns
  • result_­last for the overloads in namespace std, or
  • {last1, last2, result_­last} for the overloads in namespace ranges.
Complexity: At most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons and applications of each projection.
Remarks: Stable ([algorithm.stable]).
If [first1, last1) contains m elements that are equivalent to each other and [first2, last2) contains n elements that are equivalent to them, then all m elements from the first range are copied to the output range, in order, and then the final elements from the second range are copied to the output range, in order.

24.7.6.3 set_­intersection [set.intersection]

template<class InputIterator1, class InputIterator2, class OutputIterator> constexpr OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator> ForwardIterator set_intersection(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result); template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> constexpr OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator, class Compare> ForwardIterator set_intersection(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result, Compare comp); namespace ranges { template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<I1, I2, O, Comp, Proj1, Proj2> constexpr set_intersection_result<I1, I2, O> set_intersection(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<InputRange R1, InputRange R2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2> constexpr set_intersection_result<safe_iterator_t<R1>, safe_iterator_t<R2>, O> set_intersection(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); }
Let comp be less{}, and proj1 and proj2 be identity{} for the overloads with no parameters by those names.
Requires: The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp and proj1 or proj2, respectively.
The resulting range shall not overlap with either of the original ranges.
Effects: Constructs a sorted intersection of the elements from the two ranges; that is, the set of elements that are present in both of the ranges.
Returns: Let result_­last be the end of the constructed range.
Returns
  • result_­last for the overloads in namespace std, or
  • {last1, last2, result_­last} for the overloads in namespace ranges.
Complexity: At most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons and applications of each projection.
Remarks: Stable ([algorithm.stable]).
If [first1, last1) contains m elements that are equivalent to each other and [first2, last2) contains n elements that are equivalent to them, the first elements are copied from the first range to the output range, in order.

24.7.6.4 set_­difference [set.difference]

template<class InputIterator1, class InputIterator2, class OutputIterator> constexpr OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator> ForwardIterator set_difference(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result); template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> constexpr OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator, class Compare> ForwardIterator set_difference(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result, Compare comp); namespace ranges { template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<I1, I2, O, Comp, Proj1, Proj2> constexpr set_difference_result<I1, O> set_difference(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<InputRange R1, InputRange R2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2> constexpr set_difference_result<safe_iterator_t<R1>, O> set_difference(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); }
Let comp be less{}, and proj1 and proj2 be identity{} for the overloads with no parameters by those names.
Requires: The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp and proj1 or proj2, respectively.
The resulting range shall not overlap with either of the original ranges.
Effects: Copies the elements of the range [first1, last1) which are not present in the range [first2, last2) to the range beginning at result.
The elements in the constructed range are sorted.
Returns: Let result_­last be the end of the constructed range.
Returns
  • result_­last for the overloads in namespace std, or
  • {last1, result_­last} for the overloads in namespace ranges.
Complexity: At most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons and applications of each projection.
Remarks: If [first1, last1) contains m elements that are equivalent to each other and [first2, last2) contains n elements that are equivalent to them, the last elements from [first1, last1) is copied to the output range, in order.

24.7.6.5 set_­symmetric_­difference [set.symmetric.difference]

template<class InputIterator1, class InputIterator2, class OutputIterator> constexpr OutputIterator set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator> ForwardIterator set_symmetric_difference(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result); template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> constexpr OutputIterator set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator, class Compare> ForwardIterator set_symmetric_difference(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, ForwardIterator result, Compare comp); namespace ranges { template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<I1, I2, O, Comp, Proj1, Proj2> constexpr set_symmetric_difference_result<I1, I2, O> set_symmetric_difference(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<InputRange R1, InputRange R2, WeaklyIncrementable O, class Comp = ranges::less<>, class Proj1 = identity, class Proj2 = identity> requires Mergeable<iterator_t<R1>, iterator_t<R2>, O, Comp, Proj1, Proj2> constexpr set_symmetric_difference_result<safe_iterator_t<R1>, safe_iterator_t<R2>, O> set_symmetric_difference(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); }
Let comp be less{}, and proj1 and proj2 be identity{} for the overloads with no parameters by those names.
Requires: The resulting range shall not overlap with either of the original ranges.
The ranges [first1, last1) and [first2, last2) shall be sorted with respect to comp and proj1 or proj2, respectively.
The resulting range shall not overlap with either of the original ranges.
Effects: Copies the elements of the range [first1, last1) that are not present in the range [first2, last2), and the elements of the range [first2, last2) that are not present in the range [first1, last1) to the range beginning at result.
The elements in the constructed range are sorted.
Returns: Let result_­last be the end of the constructed range.
Returns
  • result_­last for the overloads in namespace std, or
  • {last1, last2, result_­last} for the overloads in namespace ranges.
Complexity: At most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons and applications of each projection.
Remarks: Stable ([algorithm.stable]).
If [first1, last1) contains m elements that are equivalent to each other and [first2, last2) contains n elements that are equivalent to them, then of those elements shall be copied to the output range: the last of these elements from [first1, last1) if , and the last of these elements from [first2, last2) if .
In either case, the elements are copied in order.

24.7.7 Heap operations [alg.heap.operations]

A random access range [a, b) is a heap with respect to comp and proj for a comparator and projection comp and proj if its elements are organized such that:
  • With N = b - a, for all i, , bool(invoke(comp, invoke(proj, a[]), invoke(​proj, a[i]))) is false.
  • *a may be removed by pop_­heap, or a new element added by push_­heap, in time.
These properties make heaps useful as priority queues.
make_­heap converts a range into a heap and sort_­heap turns a heap into a sorted sequence.

24.7.7.1 push_­heap [push.heap]

template<class RandomAccessIterator> constexpr void push_heap(RandomAccessIterator first, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> constexpr void push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr I push_heap(I first, S last, Comp comp = {}, Proj proj = {}); template<RandomAccessRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr safe_iterator_t<R> push_heap(R&& r, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Requires: The range [first, last - 1) shall be a valid heap with respect to comp and proj.
For the overloads in namespace std, the type of *first shall meet the Cpp17MoveConstructible requirements (Table 26) and the Cpp17MoveAssignable requirements (Table 28).
Effects: Places the value in the location last - 1 into the resulting heap [first, last).
Returns: last for the overloads in namespace ranges.
Complexity: At most comparisons and twice as many projections.

24.7.7.2 pop_­heap [pop.heap]

template<class RandomAccessIterator> constexpr void pop_heap(RandomAccessIterator first, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> constexpr void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr I pop_heap(I first, S last, Comp comp = {}, Proj proj = {}); template<RandomAccessRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr safe_iterator_t<R> pop_heap(R&& r, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Requires: The range [first, last) shall be a valid non-empty heap with respect to comp and proj.
For the overloads in namespace std, RandomAccessIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: Swaps the value in the location first with the value in the location last - 1 and makes [first, last - 1) into a heap with respect to comp and proj.
Returns: last for the overloads in namespace ranges.
Complexity: At most comparisons and twice as many projections.

24.7.7.3 make_­heap [make.heap]

template<class RandomAccessIterator> constexpr void make_heap(RandomAccessIterator first, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> constexpr void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr I make_heap(I first, S last, Comp comp = {}, Proj proj = {}); template<RandomAccessRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr safe_iterator_t<R> make_heap(R&& r, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Requires: For the overloads in namespace std, the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: Constructs a heap with respect to comp and proj out of the range [first, last).
Returns: last for the overloads in namespace ranges.
Complexity: At most comparisons and twice as many projections.

24.7.7.4 sort_­heap [sort.heap]

template<class RandomAccessIterator> constexpr void sort_heap(RandomAccessIterator first, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> constexpr void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr I sort_heap(I first, S last, Comp comp = {}, Proj proj = {}); template<RandomAccessRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr safe_iterator_t<R> sort_heap(R&& r, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Requires: The range [first, last) shall be a valid heap with respect to comp and proj.
For the overloads in namespace std, RandomAccessIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: Sorts elements in the heap [first, last) with respect to comp and proj.
Returns: last for the overloads in namespace ranges.
Complexity: At most comparisons, where , and twice as many projections.

24.7.7.5 is_­heap [is.heap]

template<class RandomAccessIterator> constexpr bool is_heap(RandomAccessIterator first, RandomAccessIterator last);
Effects: Equivalent to: return is_­heap_­until(first, last) == last;
template<class ExecutionPolicy, class RandomAccessIterator> bool is_heap(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last);
Effects: Equivalent to:
return is_heap_until(std::forward<ExecutionPolicy>(exec), first, last) == last;
template<class RandomAccessIterator, class Compare> constexpr bool is_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
Effects: Equivalent to: return is_­heap_­until(first, last, comp) == last;
template<class ExecutionPolicy, class RandomAccessIterator, class Compare> bool is_heap(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last, Compare comp);
Effects: Equivalent to:
return is_heap_until(std::forward<ExecutionPolicy>(exec), first, last, comp) == last;
namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less<>> constexpr bool is_heap(I first, S last, Comp comp = {}, Proj proj = {}); template<RandomAccessRange R, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<R>, Proj>> Comp = ranges::less<>> constexpr bool is_heap(R&& r, Comp comp = {}, Proj proj = {}); }
Effects: Equivalent to: return ranges::is_­heap_­until(first, last, comp, proj) == last;
template<class RandomAccessIterator> constexpr RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last); template<class ExecutionPolicy, class RandomAccessIterator> RandomAccessIterator is_heap_until(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> constexpr RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last, Compare comp); template<class ExecutionPolicy, class RandomAccessIterator, class Compare> RandomAccessIterator is_heap_until(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last, Compare comp); namespace ranges { template<RandomAccessIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less<>> constexpr I is_heap_until(I first, S last, Comp comp = {}, Proj proj = {}); template<RandomAccessRange R, class Proj = identity, IndirectStrictWeakOrder<projected<iterator_t<R>, Proj>> Comp = ranges::less<>> constexpr safe_iterator_t<R> is_heap_until(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 last iterator i in [first, last] for which the range [first, i) is a heap with respect to comp and proj.
Complexity: Linear.

24.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); namespace ranges { template<class T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less<>> constexpr const T& 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); namespace ranges { template<Copyable T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less<>> constexpr T 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>> 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); namespace ranges { template<class T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less<>> constexpr const T& 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); namespace ranges { template<Copyable T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less<>> constexpr T 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>> 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); namespace ranges { template<class T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less<>> constexpr minmax_result<const T&> 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); namespace ranges { template<Copyable T, class Proj = identity, IndirectStrictWeakOrder<projected<const T*, Proj>> Comp = ranges::less<>> constexpr minmax_result<T> 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 minmax_result<iter_value_t<iterator_t<R>>> 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); namespace ranges { template<ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less<>> constexpr I 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> 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); namespace ranges { template<ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less<>> constexpr I 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> 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); namespace ranges { template<ForwardIterator I, Sentinel<I> S, class Proj = identity, IndirectStrictWeakOrder<projected<I, Proj>> Comp = ranges::less<>> constexpr minmax_result<I> 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 minmax_result<safe_iterator_t<R>> 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 iterator237 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().

24.7.9 Bounded value [alg.clamp]

template<class T> constexpr const T& clamp(const T& v, const T& lo, const T& hi); template<class T, class Compare> constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp);
Requires: The value of lo shall be no greater than hi.
For the first form, type T shall be Cpp17LessThanComparable (Table 24).
Returns: lo if v is less than lo, hi if hi is less than v, otherwise v.
[Note
:
If NaN is avoided, T can be a floating-point type.
end note
]
Complexity: At most two comparisons.

24.7.10 Lexicographical comparison [alg.lex.comparison]

template<class InputIterator1, class InputIterator2> constexpr bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> bool lexicographical_compare(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template<class InputIterator1, class InputIterator2, class Compare> constexpr bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Compare> bool lexicographical_compare(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, Compare comp); namespace ranges { template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2, class Proj1 = identity, class Proj2 = identity, IndirectStrictWeakOrder<projected<I1, Proj1>, projected<I2, Proj2>> Comp = ranges::less<>> constexpr bool lexicographical_compare(I1 first1, S1 last1, I2 first2, S2 last2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<InputRange R1, InputRange R2, class Proj1 = identity, class Proj2 = identity, IndirectStrictWeakOrder<projected<iterator_t<R1>, Proj1>, projected<iterator_t<R2>, Proj2>> Comp = ranges::less<>> constexpr bool lexicographical_compare(R1&& r1, R2&& r2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); }
Returns: true if and only if the sequence of elements defined by the range [first1, last1) is lexicographically less than the sequence of elements defined by the range [first2, last2).
Complexity: At most applications of the corresponding comparison and each projection, if any.
Remarks: If two sequences have the same number of elements and their corresponding elements (if any) are equivalent, then neither sequence is lexicographically less than the other.
If one sequence is a proper prefix of the other, then the shorter sequence is lexicographically less than the longer sequence.
Otherwise, the lexicographical comparison of the sequences yields the same result as the comparison of the first corresponding pair of elements that are not equivalent.
[Example
:
ranges::lexicographical_­compare(I1, S1, I2, S2, Comp, Proj1, Proj2) could be implemented as:
for ( ; first1 != last1 && first2 != last2 ; ++first1, (void) ++first2) {
  if (invoke(comp, invoke(proj1, *first1), invoke(proj2, *first2))) return true;
  if (invoke(comp, invoke(proj2, *first2), invoke(proj1, *first1))) return false;
}
return first1 == last1 && first2 != last2;
end example
]
[Note
:
An empty sequence is lexicographically less than any non-empty sequence, but not less than any empty sequence.
end note
]

24.7.11 Three-way comparison algorithms [alg.3way]

template<class T, class U> constexpr auto compare_3way(const T& a, const U& b);
Effects: Compares two values and produces a result of the strongest applicable comparison category type:
  • Returns a <=> b if that expression is well-formed.
  • Otherwise, if the expressions a == b and a < b are each well-formed and convertible to bool, returns strong_­ordering::equal when a == b is true, otherwise returns strong_­ordering::less when a < b is true, and otherwise returns strong_­ordering::greater.
  • Otherwise, if the expression a == b is well-formed and convertible to bool, returns strong_­equality::equal when a == b is true, and otherwise returns strong_­equality::nonequal.
  • Otherwise, the function is defined as deleted.
template<class InputIterator1, class InputIterator2, class Cmp> constexpr auto lexicographical_compare_3way(InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, Cmp comp) -> common_comparison_category_t<decltype(comp(*b1, *b2)), strong_ordering>;
Requires: Cmp shall be a function object type whose return type is a comparison category type.
Effects: Lexicographically compares two ranges and produces a result of the strongest applicable comparison category type.
Equivalent to:
for ( ; b1 != e1 && b2 != e2; void(++b1), void(++b2) )
  if (auto cmp = comp(*b1,*b2); cmp != 0)
    return cmp;
return b1 != e1 ? strong_ordering::greater :
       b2 != e2 ? strong_ordering::less :
                  strong_ordering::equal;
template<class InputIterator1, class InputIterator2> constexpr auto lexicographical_compare_3way(InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2);
Effects: Equivalent to:
return lexicographical_compare_3way(b1, e1, b2, e2,
                                    [](const auto& t, const auto& u) {
                                      return compare_3way(t, u);
                                    });

24.7.12 Permutation generators [alg.permutation.generators]

template<class BidirectionalIterator> constexpr bool next_permutation(BidirectionalIterator first, BidirectionalIterator last); template<class BidirectionalIterator, class Compare> constexpr bool next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); namespace ranges { template<BidirectionalIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr bool next_permutation(I first, S last, Comp comp = {}, Proj proj = {}); template<BidirectionalRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr bool next_permutation(R&& r, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
Requires: For the overloads in namespace std, BidirectionalIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]).
Effects: Takes a sequence defined by the range [first, last) and transforms it into the next permutation.
The next permutation is found by assuming that the set of all permutations is lexicographically sorted with respect to comp and proj.
If no such permutation exists, transforms the sequence into the first permutation; that is, the ascendingly-sorted one.
Returns: true if and only if a next permutation was found.
Complexity: At most (last - first) / 2 swaps.
template<class BidirectionalIterator> constexpr bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last); template<class BidirectionalIterator, class Compare> constexpr bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); namespace ranges { template<BidirectionalIterator I, Sentinel<I> S, class Comp = ranges::less<>, class Proj = identity> requires Sortable<I, Comp, Proj> constexpr bool prev_permutation(I first, S last, Comp comp = {}, Proj proj = {}); template<BidirectionalRange R, class Comp = ranges::less<>, class Proj = identity> requires Sortable<iterator_t<R>, Comp, Proj> constexpr bool prev_permutation(R&& r, Comp comp = {}, Proj proj = {}); }
Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
Requires: For the overloads in namespace std, BidirectionalIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]).
Effects: Takes a sequence defined by the range [first, last) and transforms it into the previous permutation.
The previous permutation is found by assuming that the set of all permutations is lexicographically sorted with respect to comp and proj.
If no such permutation exists, transforms the sequence into the last permutation; that is, the descendingly-sorted one.
Returns: true if and only if a previous permutation was found.
Complexity: At most (last - first) / 2 swaps.