In C++, std::copy() copies every element from a source range into a destination, starting at the destination iterator you give it.
The destination can be another vector, an array, or even a stream. The source stays unchanged. Make sure the destination has enough room, or use std::back_inserter so it grows by itself.
Syntax
Add #include <algorithm>, then call std::copy(). Start with these two lines.
std::ostream_iterator<int> is an output iterator that writes to a stream.
The second argument, " | ", is printed after each number.
So copy() can print a whole container with one line.
Frequently Asked Questions
In C++, std::copy() copies the elements of one range into another place. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the position just after the last copied element in the destination.
A copy constructor makes a whole new vector. std::copy() copies elements into a destination that already exists, and works with any container or array.
The destination must not start inside the source range. Use std::copy_backward() when the destination overlaps on the right.