Skip to content
Related Articles

Related Articles

Ways to copy a vector in C++

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 22 Jun, 2022

In the case of arrays, there is not much choice to copy an array into another, other than the iterative method i.e running a loop to copy each element at its respective index. But Vector classes have more than one method for copying entire vectors into others in easier ways.

 

CPP-STL-Self-Paced-Course 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 the new vector. They are deeply copied 

CPP




// C++ code to demonstrate copy of vector
// by iterative method.
#include<iostream>
#include<vector>
using namespace std;
 
int main()
{
    // Initializing vector with values
    vector<int> vect1{1, 2, 3, 4};
 
    // Declaring new vector
    vector<int> vect2;
 
    // A loop to copy elements of
    // old vector into new vector
    // by Iterative method
    for (int i=0; i<vect1.size(); i++)
        vect2.push_back(vect1[i]);
 
    cout << "Old vector elements are : ";
    for (int i=0; i<vect1.size(); i++)
        cout << vect1[i] << " ";
    cout << endl;
 
    cout << "New vector elements are : ";
    for (int i=0; i<vect2.size(); i++)
        cout << vect2[i] << " ";
    cout<< endl;
 
    // Changing value of vector to show that a new
    // copy is created.
    vect1[0] = 2;
 
    cout << "The first element of old vector is :";
    cout << vect1[0] << endl;
    cout << "The first element of new vector is :";
    cout << vect2[0] <<endl;
 
    return 0;
}


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, changing the value at one vector did not alter the value at another vector, hence they are not allocated at the same address, hence deep copy.

Method 2: By assignment “=” operator. Simply assigning the new vector to the old one copies the vector. This way of assignment is not possible in the case of arrays. 

CPP




// C++ code to demonstrate copy of vector
// by iterative method.
#include<iostream>
#include<vector>
using namespace std;
 
int main()
{
    // Initializing vector with values
    vector<int> vect1{1, 2, 3, 4};
 
    // Declaring new vector
    vector<int> vect2;
 
    // Using assignment operator to copy one
    // vector to other
    vect2 = vect1;
 
    cout << "Old vector elements are : ";
    for (int i=0; i<vect1.size(); i++)
        cout << vect1[i] << " ";
    cout << endl;
 
    cout << "New vector elements are : ";
    for (int i=0; i<vect2.size(); i++)
        cout << vect2[i] << " ";
    cout<< endl;
 
    // Changing value of vector to show that a new
    // copy is created.
    vect1[0] = 2;
 
    cout << "The first element of old vector is :";
    cout << vect1[0] << endl;
    cout << "The first element of new vector is :";
    cout << vect2[0] <<endl;
 
    return 0;
}


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 the passed vector into the newly declared vector. They are deeply copied. 

CPP




// C++ code to demonstrate copy of vector
// by constructor method.
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initializing vector with values
    vector<int> vect1{1, 2, 3, 4};
 
    // Declaring new vector and copying
    // element of old vector
    // constructor method, Deep copy
    vector<int> vect2(vect1);
 
    cout << "Old vector elements are : ";
    for (int i=0; i<vect1.size(); i++)
        cout << vect1[i] << " ";
    cout << endl;
 
    cout << "New vector elements are : ";
    for (int i=0; i<vect2.size(); i++)
        cout << vect2[i] << " ";
    cout<< endl;
 
    // Changing value of vector to show that a new
    // copy is created.
    vect1[0] = 2;
 
    cout << "The first element of old vector is :";
    cout << vect1[0] << endl;
    cout << "The first element of new vector is :";
    cout << vect2[0] <<endl;
 
    return 0;
}


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: 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 the old vector, second, the last iterator of the old vector and third is back_inserter function to insert values from the back. This also generated a deep copy. 

CPP




// C++ code to demonstrate copy of vector
// by assign() and copy().
#include<iostream>
#include<vector> // for vector
#include<algorithm> // for copy() and assign()
#include<iterator> // for back_inserter
using namespace std;
int main()
{
    // Initializing vector with values
    vector<int> vect1{1, 2, 3, 4};
 
    // Declaring new vector
    vector<int> vect2;
 
    // Copying vector by copy function
    copy(vect1.begin(), vect1.end(), back_inserter(vect2));
 
    cout << "Old vector elements are : ";
    for (int i=0; i<vect1.size(); i++)
        cout << vect1[i] << " ";
    cout << endl;
 
    cout << "New vector elements are : ";
    for (int i=0; i<vect2.size(); i++)
        cout << vect2[i] << " ";
    cout<< endl;
 
    // Changing value of vector to show that a new
    // copy is created.
    vect1[0] = 2;
 
    cout << "The first element of old vector is :";
    cout << vect1[0] << endl;
    cout << "The first element of new vector is :";
    cout << vect2[0] <<endl;
 
    return 0;
}


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 5: assign(first_iterator_o, last_iterator_o):
This method assigns the same values to the new vector as the old one. This takes 2 arguments, the first iterator to the old vector and the last iterator to the old vector. This generates a deep copy. 

CPP




// C++ code to demonstrate copy of vector
// by assign()
#include<iostream>
#include<vector> // for vector
#include<algorithm> // for copy() and assign()
#include<iterator> // for back_inserter
using namespace std;
 
int main()
{
    // Initializing vector with values
    vector<int> vect1{1, 2, 3, 4};
 
    // Declaring another vector
    vector<int> vect2;
 
    // Copying vector by assign function
    vect2.assign(vect1.begin(), vect1.end());
 
    cout << "Old vector elements are : ";
    for (int i=0; i<vect1.size(); i++)
        cout << vect1[i] << " ";
    cout << endl;
 
    cout << "New vector elements are : ";
    for (int i=0; i<vect2.size(); i++)
        cout << vect2[i] << " ";
    cout<< endl;
 
    // Changing value of vector to show that a new
    // copy is created.
    vect1[0] = 2;
 
    cout << "The first element of old vector is :";
    cout << vect1[0] << endl;
    cout << "The first element of new vector is :";
    cout << vect2[0] <<endl;
 
    return 0;
}


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 6: By using insert function. The vector class has a standard function, insert(), that can insert elements from a specified range.

C++




// C++ code to demonstrate copy of vector
// by instert() function
 
#include<iostream>
#include<vector> // for vector
using namespace std;
 
int main()
{
    // Initializing vector with values
    vector<int> vect1{1, 2, 3, 4};
 
    // Declaring new vector
    vector<int> vect2;
 
    // Copying vector by insert function
    vect2.insert(vect2.begin(), vect1.begin(), vect1.end());
 
    cout << "Old vector elements are : ";
    for (int i=0; i<vect1.size(); i++)
        cout << vect1[i] << " ";
    cout << endl;
 
    cout << "New vector elements are : ";
    for (int i=0; i<vect2.size(); i++)
        cout << vect2[i] << " ";
    cout<< endl;
 
    // Changing value of vector to show that a new
    // copy is created.
    vect1[0] = 2;
 
    cout << "The first element of old vector is :";
    cout << vect1[0] << endl;
    cout << "The first element of new vector is :";
    cout << vect2[0] <<endl;
 
    return 0;
}
 
//This code is contributed by Susobhan AKhuli


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. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!