Copy a vector in C++
This post will discuss how to copy a vector in C++. Copying a vector includes constructing a new vector with a copy of each of the elements in the original vector and in the same order.
1. Using Copy Constructor
The recommended approach is to use the copy constructor, which internally takes care of all copying.
|
1 2 3 4 5 6 |
template<typename T> std::vector<T> create_copy(std::vector<T> const &vec) { std::vector<T> v(vec); return v; } |
We can also use a range constructor for this:
|
1 2 3 4 5 6 |
template<typename T> std::vector<T> create_copy(std::vector<T> const &vec) { std::vector<T> v(vec.begin(), vec.end()); return v; } |
2. Using vector::operator=
Here, the idea is to create an empty vector and use its member function operator= for copying all elements of the given vector to it.
|
1 2 3 4 5 6 7 8 |
template<typename T> std::vector<T> create_copy(std::vector<T> const &vec) { std::vector<T> v; v = vec; return v; } |
We know that an object is passed by value to a function in C++ by default. That means the stack will create the copy of the whole object if the object is passed by value. We can take advantage of this fact and force a copy of the vector, as shown below:
|
1 2 3 4 |
template<typename T> std::vector<T> create_copy(std::vector<T> const vec) { // Note: no reference return vec; } |
3. Using std::copy function
The standard algorithm for copying is std::copy. We can use it for copying elements from the source vector to the destination vector. But make sure that the destination vector has sufficient space to accommodate all elements of the source sequence.
|
1 2 3 4 5 6 7 |
template<typename T> std::vector<T> create_copy(std::vector<T> const &vec) { std::vector<T> v(vec.size()); std::copy(vec.begin(), vec.end(), v.begin()); return v; } |
If the destination vector doesn’t have enough space for copying, prefer using std::back_insert iterator, which will call vector::push_back on the destination vector, as shown below:
|
1 2 3 4 5 6 7 |
template<typename T> std::vector<T> create_copy(std::vector<T> const &vec) { std::vector<T> v; std::copy(vec.begin(), vec.end(), back_inserter(v)); return v; } |
4. Using vector::insert function
The vector class has a standard function, insert(), that can insert elements from a specified range.
|
1 2 3 4 5 6 7 8 |
template<typename T> std::vector<T> create_copy(std::vector<T> const &vec) { std::vector<T> v; v.insert(v.begin(), vec.begin(), vec.end()); return v; } |
5. Using vector::assign function
Here’s another solution using the public member function assign() of the vector class, which replaces the vector contents with contents of the specified range.
|
1 2 3 4 5 6 7 8 |
template<typename T> std::vector<T> create_copy(std::vector<T> const &vec) { std::vector<T> v; v.assign(vec.begin(), vec.end()); return v; } |
6. Using vector::push_back function
Finally, we can call vector::push_back on each of the elements in the given vector using a range-based for-loop. Simple yet efficient.
|
1 2 3 4 5 6 7 8 9 10 |
template<typename T> std::vector<T> create_copy(std::vector<T> const &vec) { std::vector<T> v; for (T const &e: vec) { v.push_back(e); } return v; } |
Important Note:
Please note that all above solutions perform a shallow copy on the vector object. C++ doesn’t offer any utility function for performing a deep copy. If a deep copy is needed, we can write our own routine, which traverses the vector and manually copy the references to other objects.
That’s all about copying a vector in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)