Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Algorithms

The std::copy and std::copy_if algorithms

Reference for C++ std::copy, copy_if, copy_n and copy_backward: copying ranges, filtering while copying, overlap rules and the ranges versions.

std::copy copies the elements of a range into an output range, one by one, from first to last. std::copy_if copies only the elements for which a predicate is true, std::copy_n copies a given number of elements, and std::copy_backward copies from the back so that overlapping ranges can be shifted to the right.

The destination is an output iterator and must already have room, unless you use an inserting iterator such as std::back_inserter or std::ostream_iterator. Copying is one of the algorithms most often combined with those adaptors.

C++
std::copy(src.begin(), src.end(), dst.begin());
std::copy_if(src.begin(), src.end(), std::back_inserter(dst), pred);

Try it

C++ Copy, filter and print

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Overlapping ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class InIt, class OutIt>
OutIt copy(InIt first, InIt last, OutIt d_first);

template<class InIt, class OutIt, class Pred>
OutIt copy_if(InIt first, InIt last, OutIt d_first, Pred pred);

template<class InIt, class Size, class OutIt>
OutIt copy_n(InIt first, Size count, OutIt d_first);

template<class BidIt1, class BidIt2>
BidIt2 copy_backward(BidIt1 first, BidIt1 last, BidIt2 d_last);
Name Description
first, last The source range [first, last).
count Number of elements for copy_n; zero or negative copies nothing.
d_first Where the first element is written.
d_last End of the destination for copy_backward; elements are written backward from here.
pred Selects which elements copy_if copies.

Returns: an iterator one past the last element written (for copy_backward, the iterator to the last element written, i.e. the start of the copied block).

Errors / undefined behavior: writing beyond the end of the destination is undefined. For copy and copy_if, d_first must not lie inside [first, last); for copy_backward, d_last must not lie inside (first, last].

Usage notes

  • Choose by direction: if the destination starts before the source (shifting left) use copy; if it starts after (shifting right) use copy_backward. Or use std::move / memmove-style helpers.
  • Size the destination: pass an already sized container, a back_inserter, or a std::ostream_iterator for printing. v.begin() of an empty vector is a bug.
  • Trivially copyable types: implementations typically reduce copy to memmove, so it is as fast as the C function.
  • Whole container: the copy constructor or assign is clearer than copying an entire container with copy.
  • copy_if is stable: the relative order of copied elements is kept. To delete from the same container rather than copy, see the erase-remove idiom.
  • Moving instead of copying: std::move (algorithm) transfers the elements.
  • Ranges (C++20): std::ranges::copy, copy_if, copy_n, copy_backward take a range and return a result struct with in and out.
  • Complexity: exactly last - first assignments (predicate calls for copy_if too).