← Patterns

Copy a range of elements

12345678910111213141516171819#include <vector> #include <algorithm> #include <iterator> std::vector<int> target2(5); std::vector<int> target3; template <typename RangeOfInts> void foo(RangeOfInts source) { std::vector<int> target1{std::begin(source), std::end(source)}; std::copy(std::begin(source), std::end(source), std::begin(target2)); std::copy(std::begin(source), std::end(source), std::back_inserter(target3)); }

This pattern is licensed under the CC0 Public Domain Dedication.

Requires c++11 or newer.

Intent

Copy elements from a range to another range or container.

Description

We start with a source range object on line 9 and, for the purposes of this example, we assume its elements are of type int.

On lines 11–12, we copy the elements from the source range into a container, target1, simply by passing the begin and end iterators of the range to the std::vector<T>’s constructor. We use std::begin and std::end to obtain these iterators.

To copy the elements of source into a range or container which already has the appropriate number of elements allocated, represented by target2 on line 5, we use std::copy on lines 14–15. The first two iterator arguments denote the source range, and the third iterator argument denotes the start of the target range. For this to work, the elements must already exist in the target range.

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

Contributors

  • Joseph Mansfield

Last Updated

09 December 2017

Source

Fork this pattern on GitHub

Share