# 25 Algorithms library [algorithms]

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

### 25.3.1 Copy [alg.copy]

``` template<class InputIterator, class OutputIterator> OutputIterator copy(InputIterator first, InputIterator last, OutputIterator 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: result + (last - first).

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

Complexity: Exactly last - first assignments.

``` template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n(InputIterator first, Size n, OutputIterator result); ```

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

Returns: result + n.

Complexity: Exactly n assignments.

``` template<class InputIterator, class OutputIterator, class Predicate> OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred); ```

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

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> BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result); ```

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

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

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).