Ways to copy a vector in C++

• Difficulty Level : Easy
• Last Updated : 28 Jun, 2021

In case of arrays, there is no much choice to copy an array into other, other than iterative method i.e running a loop to copy each element at respective index. But Vector classes has more than one methods to copy entire vector into other in easier ways.There are basically two types of copying :-

Method 1 : Iterative method.
This method is a general method to copy, in this method a loop is used to push_back() the old vector elements into new vector.They are deeply copied

 // C++ code to demonstrate copy of vector// by iterative method.#include#includeusing namespace std;  int main(){    // Initializing vector with values    vector vect1{1, 2, 3, 4};      // Declaring new vector    vector vect2;      // A loop to copy elements of    // old vector into new vector    // by Iterative method    for (int i=0; i

Output:

Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is : 2
The first element of new vector is : 1

In the above code, on changing the value at one vector did not alter value at other vector, hence they are not allocated at same address, hence deep copy.
Method 2 : By assignment “=” operator.
Simply assigning the new vector to old one copies the vector. This way of assignment is not possible in case of arrays.

 // C++ code to demonstrate copy of vector// by iterative method.#include#includeusing namespace std;  int main(){    // Initializing vector with values    vector vect1{1, 2, 3, 4};      // Declaring new vector    vector vect2;      // Using assignment operator to copy one    // vector to other    vect2 = vect1;      cout << "Old vector elements are : ";    for (int i=0; i

Output:

Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is : 2
The first element of new vector is : 1

Method 3 : By passing vector as constructor. At the time of declaration of vector, passing an old initialized vector, copies the elements of passed vector into the newly declared vector. They are deeply copied.

 // C++ code to demonstrate copy of vector// by constructor method.#includeusing namespace std;  int main(){    // Initializing vector with values    vector vect1{1, 2, 3, 4};      // Declaring new vector and copying    // element of old vector    // constructor method, Deep copy    vector vect2(vect1);      cout << "Old vector elements are : ";    for (int i=0; i

Output:

Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1

Method 4 : By using inbuilt functions

• copy(first_iterator_o, last_iterator_o, back_inserter()) :- This is another way to copy old vector into new one. This function takes 3 arguments, first, the first iterator of old vector, second, the last iterator of old vector and third is back_inserter function to insert values from back. This also
generated a deep copy.
 // C++ code to demonstrate copy of vector// by assign() and copy().#include#include // for vector#include // for copy() and assign()#include // for back_inserterusing namespace std;int main(){    // Initializing vector with values    vector vect1{1, 2, 3, 4};      // Declaring new vector    vector vect2;      // Copying vector by copy function    copy(vect1.begin(), vect1.end(), back_inserter(vect2));      cout << "Old vector elements are : ";    for (int i=0; i

Output :

Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1

• assign(first_iterator_o, last_iterator_o) :- This method assigns the same values to new vector as old one. This takes 2 arguments, first iterator to old vector and last iterator to old vector.This generates a deep copy.
 // C++ code to demonstrate copy of vector// by assign()#include#include // for vector#include // for copy() and assign()#include // for back_inserterusing namespace std;  int main(){    // Initializing vector with values    vector vect1{1, 2, 3, 4};      // Declaring another vector    vector vect2;      // Copying vector by assign function    vect2.assign(vect1.begin(), vect1.end());      cout << "Old vector elements are : ";    for (int i=0; i

Output:

Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1

This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.