23 Algorithms library [algorithms]

23.6 Mutating sequence operations [alg.modifying.operations]

23.6.1 Copy [alg.copy]

template<class InputIterator, class OutputIterator> constexpr OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);
Requires: result shall not be in the range [first, last).
Effects: Copies elements in the range [first, last) into the range [result, result + (last - first)) starting from first and proceeding to last.
For each non-negative integer n < (last - first), performs *(result + n) = *(first + n).
Returns: result + (last - first).
Complexity: Exactly last - first 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);
Effects: For each non-negative integer , performs *(result + i) = *(first + i).
Returns: result + n.
Complexity: Exactly n 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);
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 25).
end note
]
Effects: Copies all of the elements referred to by the iterator i in the range [first, last) for which pred(*i) is true.
Returns: The end of the resulting range.
Complexity: Exactly last - first applications of the corresponding predicate.
Remarks: Stable ([algorithm.stable]).
template<class BidirectionalIterator1, class BidirectionalIterator2> constexpr BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);
Requires: result shall not be in the range (first, last].
Effects: Copies elements in the range [first, last) into the range [result - (last-first), result) starting from last - 1 and proceeding to first.236
For each positive integer n <= (last - first), performs *(result - n) = *(last - n).
Returns: result - (last - first).
Complexity: Exactly last - first assignments.
copy_­backward should be used instead of copy when last is in the range [result - (last - first), result).

23.6.2 Move [alg.move]

template<class InputIterator, class OutputIterator> constexpr OutputIterator move(InputIterator first, InputIterator last, OutputIterator result);
Requires: result shall not be in the range [first, last).
Effects: Moves elements in the range [first, last) into the range [result, result + (last - first)) starting from first and proceeding to last.
For each non-negative integer n < (last-first), performs *(result + n) = std::move(*(first + n)).
Returns: result + (last - first).
Complexity: Exactly last - first move assignments.
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> ForwardIterator2 move(ExecutionPolicy&& policy, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result);
Requires: The ranges [first, last) and [result, result + (last - first)) shall not overlap.
Effects: Moves elements in the range [first, last) into the range [result, result + (last - first)).
For each non-negative integer n < (last - first), performs *(result + n) = std::​move(*(first + n)).
Returns: result + (last - first).
Complexity: Exactly last - first assignments.
template<class BidirectionalIterator1, class BidirectionalIterator2> constexpr BidirectionalIterator2 move_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);
Requires: result shall not be in the range (first, last].
Effects: Moves elements in the range [first, last) into the range [result - (last-first), result) starting from last - 1 and proceeding to first.237
For each positive integer n <= (last - first), performs *(result - n) = std::move(*(last - n)).
Returns: result - (last - first).
Complexity: Exactly last - first assignments.
move_­backward should be used instead of move when last is in the range [result - (last - first), result).

23.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);
Requires: The two ranges [first1, last1) and [first2, first2 + (last1 - first1)) shall not overlap.
*(first1 + n) shall be swappable with ([swappable.requirements]) *(first2 + n).
Effects: For each non-negative integer n < (last1 - first1) performs: swap(*(first1 + n), ​*(first2 + n)).
Returns: first2 + (last1 - first1).
Complexity: Exactly last1 - first1 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).

23.6.4 Transform [alg.transform]

template<class InputIterator, class OutputIterator, class UnaryOperation> constexpr OutputIterator transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class UnaryOperation> ForwardIterator2 transform(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, 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);
Requires: op and binary_­op shall not invalidate iterators or subranges, or modify elements in the ranges
  • [first1, last1],
  • [first2, first2 + (last1 - first1)], and
  • [result, result + (last1 - first1)].238
Effects: Assigns through every iterator i in the range [result, result + (last1 - first1)) a new corresponding value equal to op(*(first1 + (i - result))) or binary_­op(*(first1 + (i - result)), *(first2 + (i - result))).
Returns: result + (last1 - first1).
Complexity: Exactly last1 - first1 applications of op or binary_­op.
This requirement also applies to the overload with an ExecutionPolicy .
Remarks: result may be equal to first in case of unary transform, or to first1 or first2 in case of binary transform.
The use of fully closed ranges is intentional.

23.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);
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 the following corresponding conditions hold: *i == old_­value, pred(*i) != false.
Complexity: Exactly last - first applications of the corresponding predicate.
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);
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 the following corresponding conditions hold:
*(first + (i - result)) == old_value
pred(*(first + (i - result))) != false
Returns: result + (last - first).
Complexity: Exactly last - first applications of the corresponding predicate.

23.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);
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: The fill algorithms assign value through all the iterators in the range [first, last).
The fill_­n algorithms assign value through all the iterators in the range [first, first + n) if n is positive, otherwise they do nothing.
Returns: fill_­n returns first + n for non-negative values of n and first for negative values.
Complexity: Exactly last - first, n, or 0 assignments, respectively.

23.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);
Requires: gen takes no arguments, Size shall be convertible to an integral type ([conv.integral], [class.conv]).
Effects: The generate algorithms invoke the function object gen and assign the return value of gen through all the iterators in the range [first, last).
The generate_­n algorithms invoke the function object gen and assign the return value of gen through all the iterators in the range [first, first + n) if n is positive, otherwise they do nothing.
Returns: generate_­n returns first + n for non-negative values of n and first for negative values.
Complexity: Exactly last - first, n, or 0 invocations of gen and assignments, respectively.

23.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);
Requires: The type of *first shall satisfy the Cpp17MoveAssignable requirements (Table 27).
Effects: Eliminates all the elements referred to by iterator i in the range [first, last) for which the following corresponding conditions hold: *i == value, pred(*i) != false.
Returns: The end of the resulting range.
Remarks: Stable ([algorithm.stable]).
Complexity: Exactly last - first applications of the corresponding predicate.
[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);
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 is not Cpp17MoveConstructible (Table 25).
end note
]
Effects: Copies all the elements referred to by the iterator i in the range [first, last) for which the following corresponding conditions do not hold: *i == value, pred(*i) != false.
Returns: The end of the resulting range.
Complexity: Exactly last - first applications of the corresponding predicate.
Remarks: Stable ([algorithm.stable]).

23.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);
Requires: The comparison function shall be an equivalence relation.
The type of *first shall satisfy the Cpp17MoveAssignable requirements (Table 27).
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 the following conditions hold: *(i - 1) == *i or pred(*(i - 1), *i) != false.
Returns: The end of the resulting range.
Complexity: For nonempty ranges, exactly (last - first) - 1 applications of the corresponding predicate.
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);
Requires:
  • The comparison function shall be an equivalence relation.
  • The ranges [first, last) and [result, result+(last-first)) shall not overlap.
  • 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 be Cpp17CopyAssignable (Table 28).
    Otherwise, T shall be both Cpp17CopyConstructible (Table 26) and Cpp17CopyAssignable.
    [Note
    :
    For the overloads with an ExecutionPolicy, there may be a performance cost if the value type of ForwardIterator1 is not both Cpp17CopyConstructible and Cpp17CopyAssignable.
    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 the following corresponding conditions hold: *i == *(i - 1) or pred(*i, *(i - 1)) != false.
Returns: The end of the resulting range.
Complexity: For nonempty ranges, exactly last - first - 1 applications of the corresponding predicate.

23.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);
Requires: BidirectionalIterator shall satisfy the Cpp17ValueSwappable requirements ([swappable.requirements]).
Effects: For each non-negative integer i < (last - first) / 2, applies iter_­swap to all pairs of iterators first + i, (last - i) - 1.
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);
Requires: The ranges [first, last) and [result, result + (last - first)) shall not overlap.
Effects: Copies the range [first, last) to the range [result, result + (last - first)) such that for every non-negative integer i < (last - first) the following assignment takes place: *(result + (last - first) - 1 - i) = *(first + i).
Returns: result + (last - first).
Complexity: Exactly last - first assignments.

23.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);
Requires: [first, middle) and [middle, last) shall be valid ranges.
ForwardIterator shall satisfy the Cpp17ValueSwappable requirements ([swappable.requirements]).
The type of *first shall satisfy the Cpp17MoveConstructible (Table 25) and Cpp17MoveAssignable (Table 27) 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).
Returns: first + (last - middle).
Remarks: This is a left rotate.
Complexity: At most last - first swaps.
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);
Requires: The ranges [first, last) and [result, result + (last - first)) shall not overlap.
Effects: Copies the range [first, last) to the range [result, result + (last - first)) such that for each non-negative integer i < (last - first) the following assignment takes place: *(result + i) = *(first + (i + (middle - first)) % (last - first)).
Returns: result + (last - first).
Complexity: Exactly last - first assignments.

23.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 min(last - first, n) 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 satisfies 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.

23.6.13 Shuffle [alg.random.shuffle]

template<class RandomAccessIterator, class UniformRandomBitGenerator> void shuffle(RandomAccessIterator first, RandomAccessIterator last, UniformRandomBitGenerator&& g);
Requires: RandomAccessIterator shall satisfy the Cpp17ValueSwappable requirements ([swappable.requirements]).
The type remove_­reference_­t<UniformRandomBitGenerator> shall satisfy the requirements of a uniform random bit generator ([rand.req.urng]) type whose return type is convertible to iterator_­traits<RandomAccessIterator>::difference_­type.
Effects: Permutes the elements in the range [first, last) such that each possible permutation of those elements has equal probability of appearance.
Complexity: Exactly (last - first) - 1 swaps.
Remarks: 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.

23.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 satisfy 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 satisfy 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 satisfies 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.