C++: Copy a Range of Elements


On line 7, we create a std::vector<int> called source containing a sequence of values that we wish to copy.

On line 9, we have another std::vector<int>, target1, which is initialised with 5 elements. We then use std::copy on lines 11–12 to copy values from source to target1. The first two iterator arguments denote the source range, and the third iterator argument denotes the target range. We use std::begin and std::end to get these iterators for source and target1.

To demonstrate how we can copy into a container that does not yet contain any elements, we create an empty std::vector<int> called target2 on line 14. For the third argument of std::copy (lines 16–17), we call std::back_inserter to get an iterator that automatically calls push_back on target2 for each element that is copied.

#include <vector>
#include <algorithm>
#include <iterator>

int main()
	std::vector<int> source = {1, 2, 3, 4, 5};

	std::vector<int> target1(5);

	std::copy(std::begin(source), std::end(source),

	std::vector<int> target2;

	std::copy(std::begin(source), std::end(source),