Adobe Systems Incorporated


Category: algorithms Component type: function


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


Copy_n copies elements from the range [first, first + n) to the range [result, result + n). That is, it performs the assignments *result = *first, *(result + 1) = *(first + 1), and so on. Generally, for every integer i from 0 up to (but not including) n, copy_n performs the assignment *(result + i) = *(first + i). Assignments are performed in forward order, i.e. in order of increasing n. [1]

The return value is result + n.


Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h. This function is an SGI extension; it is not part of the C++ standard.

Requirements on types

  • InputIterator is a model of InputIterator.
  • OutputIterator is a model of OutputIterator.
  • Size is an integral type.
  • InputIterator's value type is convertible to a type in OutputIterator's set of value types.


  • n >= 0.
  • [first, first + n) is a valid range.
  • result is not an iterator within the range [first, first + n).
  • [result, result + n) is a valid range.


Linear. Exactly n assignments are performed.


Vector<int> V(5);
iota(V.begin(), V.end(), 1);

List<int> L(V.size());
copy_n(V.begin(), V.size(), L.begin());
assert(equal(V.begin(), V.end(), L.begin()));


[1] Copy_n is almost, but not quite, redundant. If first is an InputIterator, as opposed to a ForwardIterator, then the copy_n operation can't be expressed in terms of copy.

See also

copy, copy_backward

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google