# 28 Algorithms library [algorithms]

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

### 28.6.1 Copy [alg.copy]

```template<class InputIterator, class OutputIterator> 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> 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 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); 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 MoveConstructible (Table 23). 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.

```template<class BidirectionalIterator1, class BidirectionalIterator2> 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.263 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).