# 11 Algorithms library [algorithms]

## 11.4 Mutating sequence operations [alg.modifying.operations]

### 11.4.1 Copy [alg.copy]

``` template <InputIterator I, Sentinel<I> S, WeaklyIncrementable O> requires IndirectlyCopyable<I, O> tagged_pair<tag::in(I), tag::out(O)> copy(I first, S last, O result); template <InputRange Rng, WeaklyIncrementable O> requires IndirectlyCopyable<iterator_t<Rng>, O> tagged_pair<tag::in(safe_iterator_t<Rng>), tag::out(O)> copy(Rng&& rng, O result); ```

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: {last, result + (last - first)}.

Requires: result shall not be in the range [first,last).

Complexity: Exactly last - first assignments.

``` template <InputIterator I, WeaklyIncrementable O> requires IndirectlyCopyable<I, O> tagged_pair<tag::in(I), tag::out(O)> copy_n(I first, difference_type_t<I> n, O result); ```

Effects: For each non-negative integer i < n, performs *(result + i) = *(first + i).

Returns: {first + n, result + n}.

Complexity: Exactly n assignments.

``` template <InputIterator I, Sentinel<I> S, WeaklyIncrementable O, class Proj = identity, IndirectUnaryPredicate<projected<I, Proj>> Pred> requires IndirectlyCopyable<I, O> tagged_pair<tag::in(I), tag::out(O)> copy_if(I first, S last, O result, Pred pred, Proj proj = Proj{}); template <InputRange Rng, WeaklyIncrementable O, class Proj = identity, IndirectUnaryPredicate<projected<iterator_t<Rng>, Proj>> Pred> requires IndirectlyCopyable<iterator_t<Rng>, O> tagged_pair<tag::in(safe_iterator_t<Rng>), tag::out(O)> copy_if(Rng&& rng, O result, Pred pred, Proj proj = Proj{}); ```

Let N be the number of iterators i in the range [first,last) for which the condition invoke(pred, invoke(proj, *i)) holds.

Requires: The ranges [first,last) and [result,result + N) shall not overlap.

Effects: Copies all of the elements referred to by the iterator i in the range [first,last) for which invoke(pred, invoke(proj, *i)) is true.

Returns: {last, result + N}.

Complexity: Exactly last - first applications of the corresponding predicate and projection.

Remarks: Stable ( ISO/IEC 14882:2014 §[algorithm.stable]).

``` template <BidirectionalIterator I1, Sentinel<I1> S1, BidirectionalIterator I2> requires IndirectlyCopyable<I1, I2> tagged_pair<tag::in(I1), tag::out(I2)> copy_backward(I1 first, S1 last, I2 result); template <BidirectionalRange Rng, BidirectionalIterator I> requires IndirectlyCopyable<iterator_t<Rng>, I> tagged_pair<tag::in(safe_iterator_t<Rng>), tag::out(I)> copy_backward(Rng&& rng, I result); ```

Effects: Copies elements in the range [first,last) into the range [result - (last-first),result) starting from last - 1 and proceeding to first.5 For each positive integer n <= (last - first), performs *(result - n) = *(last - n).

Requires: result shall not be in the range (first,last].

Returns: {last, 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).