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.
std::copy(src.begin(), src.end(), dst.begin()); std::copy_if(src.begin(), src.end(), std::back_inserter(dst), pred);
Try it
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.
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
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) usecopy_backward. Or usestd::move/memmove-style helpers. - Size the destination: pass an already sized container, a
back_inserter, or astd::ostream_iteratorfor printing.v.begin()of an empty vector is a bug. - Trivially copyable types: implementations typically reduce
copytomemmove, so it is as fast as the C function. - Whole container: the copy constructor or
assignis clearer than copying an entire container withcopy. copy_ifis 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_backwardtake a range and return a result struct withinandout. - Complexity: exactly
last - firstassignments (predicate calls forcopy_iftoo).