25 Algorithms library [algorithms]

25.6 Mutating sequence operations [alg.modifying.operations]

25.6.1 Copy [alg.copy]

template<class InputIterator, class OutputIterator> constexpr OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result); template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O> requires IndirectlyCopyable<I, O> constexpr ranges::copy_result<I, O> ranges::copy(I first, S last, O result); template<InputRange R, WeaklyIncrementable O> requires IndirectlyCopyable<iterator_t<R>, O> constexpr ranges::copy_result<safe_iterator_t<R>, O> ranges::copy(R&& r, O result);
Let N be last - first.
Requires: result shall not be in the range [first, last).
Effects: Copies elements in the range [first, last) into the range [result, result + N) starting from first and proceeding to last.
For each non-negative integer , performs *(result + n) = *(first + n).
Returns:
  • result + N for the overload in namespace std, or
  • {last, result + N} for the overloads in namespace ranges.
Complexity: Exactly N assignments.
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> ForwardIterator2 copy(ExecutionPolicy&& policy, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result);
Requires: The ranges [first, last) and [result, result + (last - first)) shall not overlap.
Effects: Copies elements in the range [first, last) into the range [result, result + (last - first)).
For each non-negative integer n < (last - first), performs *(result + n) = *(first + n).
Returns: result + (last - first).
Complexity: Exactly last - first assignments.
template<class InputIterator, class Size, class OutputIterator> constexpr OutputIterator copy_n(InputIterator first, Size n, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class Size, class ForwardIterator2> ForwardIterator2 copy_n(ExecutionPolicy&& exec, ForwardIterator1 first, Size n, ForwardIterator2 result); template<InputIterator I, WeaklyIncrementable O> requires IndirectlyCopyable<I, O> constexpr ranges::copy_n_result<I, O> ranges::copy_n(I first, iter_difference_t<I> n, O result);
Let M be .
Effects: For each non-negative integer , performs *(result + i) = *(first + i).
Returns:
  • result + M for the overloads in namespace std, or
  • {first + M, result + M} for the overload in namespace ranges.
Complexity: Exactly M assignments.
template<class InputIterator, class OutputIterator, class Predicate> constexpr OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Predicate> ForwardIterator2 copy_if(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result, Predicate pred); template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> requires IndirectlyCopyable<I, O> constexpr ranges::copy_if_result<I, O> ranges::copy_if(I first, S last, O result, Pred pred, Proj proj = {}); template<InputRange R, WeaklyIncrementable O, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> requires IndirectlyCopyable<iterator_t<R>, O> constexpr ranges::copy_if_result<safe_iterator_t<R>, O> ranges::copy_if(R&& r, O result, Pred pred, Proj proj = {});
Let E be:
  • bool(pred(*i)) for the overloads in namespace std, or
  • bool(invoke(pred, invoke(proj, *i))) for the overloads in namespace ranges.
and N be the number of iterators i in the range [first, last) for which the condition E holds.
Requires: The ranges [first, last) and [result, result + (last - first)) shall not overlap.
[Note
:
For the overload with an ExecutionPolicy, there may be a performance cost if iterator_­traits<ForwardIterator1>::value_­type is not Cpp17MoveConstructible (Table 26).
end note
]
Effects: Copies all of the elements referred to by the iterator i in the range [first, last) for which E is true.
Returns:
  • result + N for the overloads in namespace std, or
  • {last, result + N} for the overloads in namespace ranges.
Complexity: Exactly last - first applications of the corresponding predicate and any projection.
Remarks: Stable ([algorithm.stable]).
template<class BidirectionalIterator1, class BidirectionalIterator2> constexpr BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result); template<BidirectionalIterator I1, Sentinel<I1> S1, BidirectionalIterator I2> requires IndirectlyCopyable<I1, I2> constexpr ranges::copy_backward_result<I1, I2> ranges::copy_backward(I1 first, S1 last, I2 result); template<BidirectionalRange R, BidirectionalIterator I> requires IndirectlyCopyable<iterator_t<R>, I> constexpr ranges::copy_backward_result<safe_iterator_t<R>, I> ranges::copy_backward(R&& r, I result);
Let N be last - first.
Requires: result shall not be in the range (first, last].
Effects: Copies elements in the range [first, last) into the range [result - N, result) starting from last - 1 and proceeding to first.235
For each positive integer , performs *(result - n) = *(last - n).
Returns:
  • result - N for the overload in namespace std, or
  • {last, result - N} for the overloads in namespace ranges.
Complexity: Exactly N assignments.
copy_­backward should be used instead of copy when last is in the range [result - N, result).

25.6.2 Move [alg.move]

template<class InputIterator, class OutputIterator> constexpr OutputIterator move(InputIterator first, InputIterator last, OutputIterator result); template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O> requires IndirectlyMovable<I, O> constexpr ranges::move_result<I, O> ranges::move(I first, S last, O result); template<InputRange R, WeaklyIncrementable O> requires IndirectlyMovable<iterator_t<R>, O> constexpr ranges::move_result<safe_iterator_t<R>, O> ranges::move(R&& r, O result);
Let E be
  • std::move(*(first + n)) for the overload in namespace std, or
  • ranges::iter_­move(first + n) for the overloads in namespace ranges.
Let N be last - first.
Requires: result shall not be in the range [first, last).
Effects: Moves elements in the range [first, last) into the range [result, result + N) starting from first and proceeding to last.
For each non-negative integer , performs *(result + n) = E.
Returns:
  • result + N for the overload in namespace std, or
  • {last, result + N} for the overloads in namespace ranges.
Complexity: Exactly N assignments.
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> ForwardIterator2 move(ExecutionPolicy&& policy, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result);
Let N be last - first.
Requires: The ranges [first, last) and [result, result + N) shall not overlap.
Effects: Moves elements in the range [first, last) into the range [result, result + N).
For each non-negative integer , performs *(result + n) = std::​move(*(first + n)).
Returns: result + N.
Complexity: Exactly N assignments.
template<class BidirectionalIterator1, class BidirectionalIterator2> constexpr BidirectionalIterator2 move_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result); template<BidirectionalIterator I1, Sentinel<I1> S1, BidirectionalIterator I2> requires IndirectlyMovable<I1, I2> constexpr ranges::move_backward_result<I1, I2> ranges::move_backward(I1 first, S1 last, I2 result); template<BidirectionalRange R, BidirectionalIterator I> requires IndirectlyMovable<iterator_t<R>, I> constexpr ranges::move_backward_result<safe_iterator_t<R>, I> ranges::move_backward(R&& r, I result);
Let E be
  • std::move(*(last - n)) for the overload in namespace std, or
  • ranges::iter_­move(last - n) for the overloads in namespace ranges.
Let N be last - first.
Requires: result shall not be in the range (first, last].
Effects: Moves elements in the range [first, last) into the range [result - N, result) starting from last - 1 and proceeding to first.236
For each positive integer , performs *(result - n) = E.
Returns:
  • result - N for the overload in namespace std, or
  • {last, result - N} for the overloads in namespace ranges.
Complexity: Exactly N assignments.
move_­backward should be used instead of move when last is in the range [result - N, result).

25.6.3 Swap [alg.swap]

template<class ForwardIterator1, class ForwardIterator2> constexpr ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> ForwardIterator2 swap_ranges(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2> requires IndirectlySwappable<I1, I2> constexpr ranges::swap_ranges_result<I1, I2> ranges::swap_ranges(I1 first1, S1 last1, I2 first2, S2 last2); template<InputRange R1, InputRange R2> requires IndirectlySwappable<iterator_t<R1>, iterator_t<R2>> constexpr ranges::swap_ranges_result<safe_iterator_t<R1>, safe_iterator_t<R2>> ranges::swap_ranges(R1&& r1, R2&& r2);
Let:
  • last2 be first2 + (last1 - first1) for the overloads with no parameter named last2, and
  • M be .
Requires: The two ranges [first1, last1) and [first2, last2) shall not overlap.
For the overloads in namespace std, *(first1 + n) shall be swappable with ([swappable.requirements]) *(first2 + n).
Effects: For each non-negative integer performs:
  • swap(*(first1 + n), *(first2 + n)) for the overloads in namespace std, or
  • ranges::iter_­swap(first1 + n, first2 + n) for the overloads in namespace ranges.
Returns:
  • last2 for the overloads in namespace std, or
  • {first1 + M, first2 + M} for the overloads in namespace ranges.
Complexity: Exactly M swaps.
template<class ForwardIterator1, class ForwardIterator2> constexpr void iter_swap(ForwardIterator1 a, ForwardIterator2 b);
Requires: a and b shall be dereferenceable.
*a shall be swappable with ([swappable.requirements]) *b.
Effects: As if by swap(*a, *b).

25.6.4 Transform [alg.transform]

template<class InputIterator, class OutputIterator, class UnaryOperation> constexpr OutputIterator transform(InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class UnaryOperation> ForwardIterator2 transform(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 result, UnaryOperation op); template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> constexpr OutputIterator transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class ForwardIterator, class BinaryOperation> ForwardIterator transform(ExecutionPolicy&& exec, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator result, BinaryOperation binary_op); template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O, CopyConstructible F, class Proj = identity> requires Writable<O, indirect_result_t<F&, projected<I, Proj>>> constexpr ranges::unary_transform_result<I, O> ranges::transform(I first1, S last1, O result, F op, Proj proj = {}); template<InputRange R, WeaklyIncrementable O, CopyConstructible F, class Proj = identity> requires Writable<O, indirect_result_t<F&, projected<iterator_t<R>, Proj>>> constexpr ranges::unary_transform_result<safe_iterator_t<R>, O> ranges::transform(R&& r, O result, F op, Proj proj = {}); template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2, WeaklyIncrementable O, CopyConstructible F, class Proj1 = identity, class Proj2 = identity> requires Writable<O, indirect_result_t<F&, projected<I1, Proj1>, projected<I2, Proj2>>> constexpr ranges::binary_transform_result<I1, I2, O> ranges::transform(I1 first1, S1 last1, I2 first2, S2 last2, O result, F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {}); template<InputRange R1, InputRange R2, WeaklyIncrementable O, CopyConstructible F, class Proj1 = identity, class Proj2 = identity> requires Writable<O, indirect_result_t<F&, projected<iterator_t<R1>, Proj1>, projected<iterator_t<R2>, Proj2>>> constexpr ranges::binary_transform_result<safe_iterator_t<R1>, safe_iterator_t<R2>, O> ranges::transform(R1&& r1, R2&& r2, O result, F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {});
Let:
  • last2 be first2 + (last1 - first1) for the overloads with parameter first2 but no parameter last2,
  • N be last1 - first1 for unary transforms, or for binary transforms, and
  • E be
    • op(*(first1 + (i - result))) for unary transforms defined in namespace std,
    • binary_­op(*(first1 + (i - result)), *(first2 + (i - result))) for binary transforms defined in namespace std,
    • invoke(op, invoke(proj, *(first1 + (i - result)))) for unary transforms defined in namespace ranges, or
    • invoke(binary_­op, invoke(proj1, *(first1 + (i - result))), invoke(proj2,
      *(first2 + (i - result))))
      for binary transforms defined in namespace ranges.
Requires: op and binary_­op shall not invalidate iterators or subranges, nor modify elements in the ranges
Effects: Assigns through every iterator i in the range [result, result + N) a new corresponding value equal to E.
Returns:
  • result + N for the overloads defined in namespace std,
  • {first1 + N, result + N} for unary transforms defined in namespace ranges, or
  • {first1 + N, first2 + N, result + N} for binary transforms defined in namespace ranges.
Complexity: Exactly N applications of op or binary_­op, and any projections.
This requirement also applies to the overload with an ExecutionPolicy.
Remarks: result may be equal to first1 or first2.
The use of fully closed ranges is intentional.

25.6.5 Replace [alg.replace]

template<class ForwardIterator, class T> constexpr void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); template<class ExecutionPolicy, class ForwardIterator, class T> void replace(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); template<class ForwardIterator, class Predicate, class T> constexpr void replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value); template<class ExecutionPolicy, class ForwardIterator, class Predicate, class T> void replace_if(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value); template<InputIterator I, Sentinel<I> S, class T1, class T2, class Proj = identity> requires Writable<I, const T2&> && IndirectRelation<ranges::equal_to, projected<I, Proj>, const T1*> constexpr I ranges::replace(I first, S last, const T1& old_value, const T2& new_value, Proj proj = {}); template<InputRange R, class T1, class T2, class Proj = identity> requires Writable<iterator_t<R>, const T2&> && IndirectRelation<ranges::equal_to, projected<iterator_t<R>, Proj>, const T1*> constexpr safe_iterator_t<R> ranges::replace(R&& r, const T1& old_value, const T2& new_value, Proj proj = {}); template<InputIterator I, Sentinel<I> S, class T, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> requires Writable<I, const T&> constexpr I ranges::replace_if(I first, S last, Pred pred, const T& new_value, Proj proj = {}); template<InputRange R, class T, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> requires Writable<iterator_t<R>, const T&> constexpr safe_iterator_t<R> ranges::replace_if(R&& r, Pred pred, const T& new_value, Proj proj = {});
Let E be
  • bool(*i == old_­value) for replace,
  • bool(pred(*i)) for replace_­if,
  • bool(invoke(proj, *i) == old_­value) for ranges::replace, or
  • bool(invoke(pred, invoke(proj, *i))) for ranges::replace_­if.
Requires: The expression *first = new_­value shall be valid.
Effects: Substitutes elements referred by the iterator i in the range [first, last) with new_­value, when E is true.
Returns: last for the overloads in namespace ranges.
Complexity: Exactly last - first applications of the corresponding predicate and any projection.
template<class InputIterator, class OutputIterator, class T> constexpr OutputIterator replace_copy(InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class T> ForwardIterator2 replace_copy(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result, const T& old_value, const T& new_value); template<class InputIterator, class OutputIterator, class Predicate, class T> constexpr OutputIterator replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Predicate, class T> ForwardIterator2 replace_copy_if(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result, Predicate pred, const T& new_value); template<InputIterator I, Sentinel<I> S, class T1, class T2, OutputIterator<const T2&> O, class Proj = identity> requires IndirectlyCopyable<I, O> && IndirectRelation<ranges::equal_to, projected<I, Proj>, const T1*> constexpr ranges::replace_copy_result<I, O> ranges::replace_copy(I first, S last, O result, const T1& old_value, const T2& new_value, Proj proj = {}); template<InputRange R, class T1, class T2, OutputIterator<const T2&> O, class Proj = identity> requires IndirectlyCopyable<iterator_t<R>, O> && IndirectRelation<ranges::equal_to, projected<iterator_t<R>, Proj>, const T1*> constexpr ranges::replace_copy_result<safe_iterator_t<R>, O> ranges::replace_copy(R&& r, O result, const T1& old_value, const T2& new_value, Proj proj = {}); template<InputIterator I, Sentinel<I> S, class T, OutputIterator<const T&> O, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> requires IndirectlyCopyable<I, O> constexpr ranges::replace_copy_if_result<I, O> ranges::replace_copy_if(I first, S last, O result, Pred pred, const T& new_value, Proj proj = {}); template<InputRange R, class T, OutputIterator<const T&> O, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> requires IndirectlyCopyable<iterator_t<R>, O> constexpr ranges::replace_copy_if_result<safe_iterator_t<R>, O> ranges::replace_copy_if(R&& r, O result, Pred pred, const T& new_value, Proj proj = {});
Let E be
  • bool(*(first + (i - result)) == old_­value) for replace_­copy,
  • bool(pred(*(first + (i - result)))) for replace_­copy_­if,
  • bool(invoke(proj, *(first + (i - result))) == old_­value) for ranges::replace_­copy,
  • bool(invoke(pred, invoke(proj, *(first + (i - result))))) for ranges::replace_­copy_­if.
Requires: The results of the expressions *first and new_­value shall be writable ([iterator.requirements.general]) to the result output iterator.
The ranges [first, last) and [result, result + (last - first)) shall not overlap.
Effects: Assigns to every iterator i in the range [result, result + (last - first)) either new_­value or *(first + (i - result)) depending on whether E holds.
Returns:
  • result + (last - first) for the overloads in namespace std, or
  • {last, result + (last - first)} for the overloads in namespace ranges.
Complexity: Exactly last - first applications of the corresponding predicate and any projection.

25.6.6 Fill [alg.fill]

template<class ForwardIterator, class T> constexpr void fill(ForwardIterator first, ForwardIterator last, const T& value); template<class ExecutionPolicy, class ForwardIterator, class T> void fill(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, const T& value); template<class OutputIterator, class Size, class T> constexpr OutputIterator fill_n(OutputIterator first, Size n, const T& value); template<class ExecutionPolicy, class ForwardIterator, class Size, class T> ForwardIterator fill_n(ExecutionPolicy&& exec, ForwardIterator first, Size n, const T& value); template<class T, OutputIterator<const T&> O, Sentinel<O> S> constexpr O ranges::fill(O first, S last, const T& value); template<class T, OutputRange<const T&> R> constexpr safe_iterator_t<R> ranges::fill(R&& r, const T& value); template<class T, OutputIterator<const T&> O> constexpr O ranges::fill_n(O first, iter_difference_t<O> n, const T& value);
Let N be for the fill_­n algorithms, and last - first for the fill algorithms.
Requires: The expression value shall be writable ([iterator.requirements.general]) to the output iterator.
The type Size shall be convertible to an integral type ([conv.integral], [class.conv]).
Effects: Assigns value through all the iterators in the range [first, first + N).
Returns: first + N.
Complexity: Exactly N assignments.

25.6.7 Generate [alg.generate]

template<class ForwardIterator, class Generator> constexpr void generate(ForwardIterator first, ForwardIterator last, Generator gen); template<class ExecutionPolicy, class ForwardIterator, class Generator> void generate(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Generator gen); template<class OutputIterator, class Size, class Generator> constexpr OutputIterator generate_n(OutputIterator first, Size n, Generator gen); template<class ExecutionPolicy, class ForwardIterator, class Size, class Generator> ForwardIterator generate_n(ExecutionPolicy&& exec, ForwardIterator first, Size n, Generator gen); template<Iterator O, Sentinel<O> S, CopyConstructible F> requires Invocable<F&> && Writable<O, invoke_result_t<F&>> constexpr O ranges::generate(O first, S last, F gen); template<class R, CopyConstructible F> requires Invocable<F&> && OutputRange<R, invoke_result_t<F&>> constexpr safe_iterator_t<R> ranges::generate(R&& r, F gen); template<Iterator O, CopyConstructible F> requires Invocable<F&> && Writable<O, invoke_result_t<F&>> constexpr O ranges::generate_n(O first, iter_difference_t<O> n, F gen);
Let N be for the generate_­n algorithms, and last - first for the generate algorithms.
Requires: Size shall be convertible to an integral type ([conv.integral], [class.conv]).
Effects: Assigns the result of successive evaluations of gen() through each iterator in the range [first, first + N).
Returns: first + N.
Complexity: Exactly N evaluations of gen() and assignments.

25.6.8 Remove [alg.remove]

template<class ForwardIterator, class T> constexpr ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value); template<class ExecutionPolicy, class ForwardIterator, class T> ForwardIterator remove(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, const T& value); template<class ForwardIterator, class Predicate> constexpr ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred); template<class ExecutionPolicy, class ForwardIterator, class Predicate> ForwardIterator remove_if(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Predicate pred); template<Permutable I, Sentinel<I> S, class T, class Proj = identity> requires IndirectRelation<ranges::equal_to, projected<I, Proj>, const T*> constexpr I ranges::remove(I first, S last, const T& value, Proj proj = {}); template<ForwardRange R, class T, class Proj = identity> requires Permutable<iterator_t<R>> && IndirectRelation<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*> constexpr safe_iterator_t<R> ranges::remove(R&& r, const T& value, Proj proj = {}); template<Permutable I, Sentinel<I> S, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> constexpr I ranges::remove_if(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> ranges::remove_if(R&& r, Pred pred, Proj proj = {});
Let E be
  • bool(*i == value) for remove,
  • bool(pred(*i)) for remove_­if,
  • bool(invoke(proj, *i) == value) for ranges::remove, or
  • bool(invoke(pred, invoke(proj, *i))) for ranges::remove_­if.
Requires: For the algorithms in namespace std, the type of *first shall meet the Cpp17MoveAssignable requirements (Table 28).
Effects: Eliminates all the elements referred to by iterator i in the range [first, last) for which E holds.
Returns: The end of the resulting range.
Remarks: Stable ([algorithm.stable]).
Complexity: Exactly last - first applications of the corresponding predicate and any projection.
[Note
:
Each element in the range [ret, last), where ret is the returned value, has a valid but unspecified state, because the algorithms can eliminate elements by moving from elements that were originally in that range.
end note
]
template<class InputIterator, class OutputIterator, class T> constexpr OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class T> ForwardIterator2 remove_copy(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result, const T& value); template<class InputIterator, class OutputIterator, class Predicate> constexpr OutputIterator remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Predicate> ForwardIterator2 remove_copy_if(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result, Predicate pred); template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O, class T, class Proj = identity> requires IndirectlyCopyable<I, O> && IndirectRelation<ranges::equal_to, projected<I, Proj>, const T*> constexpr ranges::remove_copy_result<I, O> ranges::remove_copy(I first, S last, O result, const T& value, Proj proj = {}); template<InputRange R, WeaklyIncrementable O, class T, class Proj = identity> requires IndirectlyCopyable<iterator_t<R>, O> && IndirectRelation<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*> constexpr ranges::remove_copy_result<safe_iterator_t<R>, O> ranges::remove_copy(R&& r, O result, const T& value, Proj proj = {}); template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> requires IndirectlyCopyable<I, O> constexpr ranges::remove_copy_if_result<I, O> ranges::remove_copy_if(I first, S last, O result, Pred pred, Proj proj = {}); template<InputRange R, WeaklyIncrementable O, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<R>, Proj>> Pred> requires IndirectlyCopyable<iterator_t<R>, O> constexpr ranges::remove_copy_if_result<safe_iterator_t<R>, O> ranges::remove_copy_if(R&& r, O result, Pred pred, Proj proj = {});
Let E be
  • bool(*i == value) for remove_­copy,
  • bool(pred(*i)) for remove_­copy_­if,
  • bool(invoke(proj, *i) == value) for ranges::remove_­copy, or
  • bool(invoke(pred, invoke(proj, *i))) for ranges::remove_­copy_­if.
Let N be the number of elements in [first, last) for which E is false.
Requires: The ranges [first, last) and [result, result + (last - first)) shall not overlap.
The expression *result = *first shall be valid.
[Note
:
For the overloads with an ExecutionPolicy, there may be a performance cost if iterator_­traits<ForwardIterator1>::value_­type does not meet the Cpp17MoveConstructible (Table 26) requirements.
end note
]
Effects: Copies all the elements referred to by the iterator i in the range [first, last) for which E is false.
Returns:
  • result + N, for the algorithms in namespace std, or
  • {last, result + N}, for the algorithms in namespace ranges.
Complexity: Exactly last - first applications of the corresponding predicate and any projection.
Remarks: Stable ([algorithm.stable]).

25.6.9 Unique [alg.unique]

template<class ForwardIterator> constexpr ForwardIterator unique(ForwardIterator first, ForwardIterator last); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator unique(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last); template<class ForwardIterator, class BinaryPredicate> constexpr ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred); template<class ExecutionPolicy, class ForwardIterator, class BinaryPredicate> ForwardIterator unique(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, BinaryPredicate pred); template<Permutable I, Sentinel<I> S, class Proj = identity, IndirectRelation<projected<I, Proj>> C = ranges::equal_to> constexpr I ranges::unique(I first, S last, C comp = {}, Proj proj = {}); template<ForwardRange R, class Proj = identity, IndirectRelation<projected<iterator_t<R>, Proj>> C = ranges::equal_to> requires Permutable<iterator_t<R>> constexpr safe_iterator_t<R> ranges::unique(R&& r, C comp = {}, Proj proj = {});
Let pred be equal_­to{} for the overloads with no parameter pred, and let E be
  • bool(pred(*(i - 1), *i)) for the overloads in namespace std, or
  • bool(invoke(comp, invoke(proj, *(i - 1)), invoke(proj, *i))) for the overloads in namespace ranges.
Requires: For the overloads in namepace std, pred shall be an equivalence relation and the type of *first shall meet the Cpp17MoveAssignable requirements (Table 28).
Effects: For a nonempty range, eliminates all but the first element from every consecutive group of equivalent elements referred to by the iterator i in the range [first + 1, last) for which E is true.
Returns: The end of the resulting range.
Complexity: For nonempty ranges, exactly (last - first) - 1 applications of the corresponding predicate and no more than twice as many applications of any projection.
template<class InputIterator, class OutputIterator> constexpr OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> ForwardIterator2 unique_copy(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result); template<class InputIterator, class OutputIterator, class BinaryPredicate> constexpr OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator2 unique_copy(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result, BinaryPredicate pred); template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O, class Proj = identity, IndirectRelation<projected<I, Proj>> C = ranges::equal_to> requires IndirectlyCopyable<I, O> && (ForwardIterator<I> || (InputIterator<O> && Same<iter_value_t<I>, iter_value_t<O>>) || IndirectlyCopyableStorable<I, O>) constexpr ranges::unique_copy_result<I, O> ranges::unique_copy(I first, S last, O result, C comp = {}, Proj proj = {}); template<InputRange R, WeaklyIncrementable O, class Proj = identity, IndirectRelation<projected<iterator_t<R>, Proj>> C = ranges::equal_to> requires IndirectlyCopyable<iterator_t<R>, O> && (ForwardIterator<iterator_t<R>> || (InputIterator<O> && Same<iter_value_t<iterator_t<R>>, iter_value_t<O>>) || IndirectlyCopyableStorable<iterator_t<R>, O>) constexpr ranges::unique_copy_result<safe_iterator_t<R>, O> ranges::unique_copy(R&& r, O result, C comp = {}, Proj proj = {});
Let pred be equal_­to{} for the overloads in namespace std with no parameter pred, and let E be
  • bool(pred(*i, *(i - 1))) for the overloads in namespace std, or
  • bool(invoke(comp, invoke(proj, *i), invoke(proj, *(i - 1)))) for the overloads in namespace ranges.
Requires:
  • The ranges [first, last) and [result, result+(last-first)) shall not overlap.
  • For the overloads in namespace std:
    • The comparison function shall be an equivalence relation.
    • The expression *result = *first shall be valid.
    • For the overloads with no ExecutionPolicy, let T be the value type of InputIterator. If InputIterator meets the Cpp17ForwardIterator requirements, then there are no additional requirements for T. Otherwise, if OutputIterator meets the Cpp17ForwardIterator requirements and its value type is the same as T, then T shall meet the Cpp17CopyAssignable (Table 29) requirements. Otherwise, T shall meet both the Cpp17CopyConstructible (Table 27) and Cpp17CopyAssignable requirements.
      [Note
      : For the overloads with an ExecutionPolicy, there may be a performance cost if the value type of ForwardIterator1 does not meet both the Cpp17CopyConstructible and Cpp17CopyAssignable requirements. —end note
      ]
Effects: Copies only the first element from every consecutive group of equal elements referred to by the iterator i in the range [first, last) for which E holds.
Returns:
  • result + N for the overloads in namespace std, or
  • {last, result + N} for the overloads in namespace ranges
Complexity: Exactly last - first - 1 applications of the corresponding predicate and no more than twice as many applications of any projection.

25.6.10 Reverse [alg.reverse]

template<class BidirectionalIterator> constexpr void reverse(BidirectionalIterator first, BidirectionalIterator last); template<class ExecutionPolicy, class BidirectionalIterator> void reverse(ExecutionPolicy&& exec, BidirectionalIterator first, BidirectionalIterator last); template<BidirectionalIterator I, Sentinel<I> S> requires Permutable<I> constexpr I ranges::reverse(I first, S last); template<BidirectionalRange R> requires Permutable<iterator_t<R>> constexpr safe_iterator_t<R> ranges::reverse(R&& r);
Requires: For the overloads in namespace std, BidirectionalIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]).
Effects: For each non-negative integer i < (last - first) / 2, applies std::iter_­swap, or ranges::​iter_­swap for the overloads in namespace ranges, to all pairs of iterators first + i, (last - i) - 1.
Returns: last for the overloads in namespace ranges.
Complexity: Exactly (last - first)/2 swaps.
template<class BidirectionalIterator, class OutputIterator> constexpr OutputIterator reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result); template<class ExecutionPolicy, class BidirectionalIterator, class ForwardIterator> ForwardIterator reverse_copy(ExecutionPolicy&& exec, BidirectionalIterator first, BidirectionalIterator last, ForwardIterator result); template<BidirectionalIterator I, Sentinel<I> S, WeaklyIncrementable O> requires IndirectlyCopyable<I, O> constexpr ranges::reverse_copy_result<I, O> ranges::reverse_copy(I first, S last, O result); template<BidirectionalRange R, WeaklyIncrementable O> requires IndirectlyCopyable<iterator_t<R>, O> constexpr ranges::reverse_copy_result<safe_iterator_t<R>, O> ranges::reverse_copy(R&& r, O result);
Let N be last - first.
Requires: The ranges [first, last) and [result, result + N) shall not overlap.
Effects: Copies the range [first, last) to the range [result, result + N) such that for every non-negative integer i < N the following assignment takes place: *(result + N - 1 - i) = *(first + i).
Returns:
  • result + N for the overloads in namespace std, or
  • {last, result + N} for the overloads in namespace ranges.
Complexity: Exactly N assignments.

25.6.11 Rotate [alg.rotate]

template<class ForwardIterator> constexpr ForwardIterator rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator rotate(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator middle, ForwardIterator last); template<Permutable I, Sentinel<I> S> constexpr subrange<I> ranges::rotate(I first, I middle, S last);
Requires: [first, middle) and [middle, last) shall be valid ranges.
For the overloads in namespace std, ForwardIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]), and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: For each non-negative integer i < (last - first), places the element from the position first + i into position first + (i + (last - middle)) % (last - first).
[Note
:
This is a left rotate.
end note
]
Returns:
  • first + (last - middle) for the overloads in namespace std, or
  • {first + (last - middle), last} for the overload in namespace ranges.
Complexity: At most last - first swaps.
template<ForwardRange R> requires Permutable<iterator_t<R>> constexpr safe_subrange_t<R> ranges::rotate(R&& r, iterator_t<R> middle);
Effects: Equivalent to: return ranges::rotate(ranges::begin(r), middle, ranges::end(r));
template<class ForwardIterator, class OutputIterator> constexpr OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> ForwardIterator2 rotate_copy(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 middle, ForwardIterator1 last, ForwardIterator2 result); template<ForwardIterator I, Sentinel<I> S, WeaklyIncrementable O> requires IndirectlyCopyable<I, O> constexpr ranges::rotate_copy_result<I, O> ranges::rotate_copy(I first, I middle, S last, O result);
Let N be last - first.
Requires: [first, middle) and [middle, last) shall be valid ranges.
The ranges [first, last) and [result, result + N) shall not overlap.
Effects: Copies the range [first, last) to the range [result, result + N) such that for each non-negative integer the following assignment takes place: *(result + i) = *(first + (i + (middle - first)) % N).
Returns:
  • result + N for the overloads in namespace std, or
  • {last, result + N} for the overload in namespace ranges.
Complexity: Exactly N assignments.
template<ForwardRange R, WeaklyIncrementable O> requires IndirectlyCopyable<iterator_t<R>, O> constexpr ranges::rotate_copy_result<safe_iterator_t<R>, O> ranges::rotate_copy(R&& r, iterator_t<R> middle, O result);
Effects: Equivalent to:
return ranges::rotate_copy(ranges::begin(r), middle, ranges::end(r), result);

25.6.12 Sample [alg.random.sample]

template<class PopulationIterator, class SampleIterator, class Distance, class UniformRandomBitGenerator> SampleIterator sample(PopulationIterator first, PopulationIterator last, SampleIterator out, Distance n, UniformRandomBitGenerator&& g);
Requires:
Effects: Copies elements (the sample) from [first, last) (the population) to out such that each possible sample has equal probability of appearance.
[Note
:
Algorithms that obtain such effects include selection sampling and reservoir sampling.
end note
]
Returns: The end of the resulting sample range.
Complexity: .
Remarks:
  • Stable if and only if PopulationIterator meets the Cpp17ForwardIterator requirements.
  • To the extent that the implementation of this function makes use of random numbers, the object g shall serve as the implementation's source of randomness.

25.6.13 Shuffle [alg.random.shuffle]

template<class RandomAccessIterator, class UniformRandomBitGenerator> void shuffle(RandomAccessIterator first, RandomAccessIterator last, UniformRandomBitGenerator&& g); template<RandomAccessIterator I, Sentinel<I> S, class Gen> requires Permutable<I> && UniformRandomBitGenerator<remove_reference_t<Gen>> I ranges::shuffle(I first, S last, Gen&& g); template<RandomAccessRange R, class Gen> requires Permutable<iterator_t<R>> && UniformRandomBitGenerator<remove_reference_t<Gen>> safe_iterator_t<R> ranges::shuffle(R&& r, Gen&& g);
Requires: For the overload in namespace std:
Effects: Permutes the elements in the range [first, last) such that each possible permutation of those elements has equal probability of appearance.
Returns: last for the overloads in namespace ranges.
Complexity: Exactly (last - first) - 1 swaps.
Remarks: To the extent that the implementation of this function makes use of random numbers, the object referenced by g shall serve as the implementation's source of randomness.

25.6.14 Shift [alg.shift]

template<class ForwardIterator> constexpr ForwardIterator shift_left(ForwardIterator first, ForwardIterator last, typename iterator_traits<ForwardIterator>::difference_type n); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator shift_left(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, typename iterator_traits<ForwardIterator>::difference_type n);
Requires: The type of *first shall meet the Cpp17MoveAssignable requirements.
Effects: If n <= 0 or n >= last - first, does nothing.
Otherwise, moves the element from position first + n + i into position first + i for each non-negative integer i < (last - first) - n.
In the first overload case, does so in order starting from i = 0 and proceeding to i = (last - first) - n - 1.
Returns: first + (last - first - n) if n is positive and n < last - first, otherwise first if n is positive, otherwise last.
Complexity: At most (last - first) - n assignments.
template<class ForwardIterator> constexpr ForwardIterator shift_right(ForwardIterator first, ForwardIterator last, typename iterator_traits<ForwardIterator>::difference_type n); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator shift_right(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, typename iterator_traits<ForwardIterator>::difference_type n);
Requires: The type of *first shall meet the Cpp17MoveAssignable requirements.
ForwardIterator shall meet the Cpp17BidirectionalIterator requirements ([bidirectional.iterators]) or the Cpp17ValueSwappable requirements.
Effects: If n <= 0 or n >= last - first, does nothing.
Otherwise, moves the element from position first + i into position first + n + i for each non-negative integer i < (last - first) - n.
In the first overload case, if ForwardIterator meets the Cpp17BidirectionalIterator requirements, does so in order starting from i = (last - first) - n - 1 and proceeding to i = 0.
Returns: first + n if n is positive and n < last - first, otherwise last if n is positive, otherwise first.
Complexity: At most (last - first) - n assignments or swaps.