Copy elements of one vector to another in C++

In this tutorial, let’s discuss copying elements of one vector into another vector in C++ programming. Before going into the topic first let me give you a small introduction regarding vectors.

Vectors in C++

We all know arrays right, where elements are stored in a continuous memory location but, the only problem with arrays is that it’s static, which means they are not resizable. this leads to wastage of memory sometimes and insufficient memory in other scenarios. So, In order to avoid this problem C++ provides us with another data structure called vectors also called dynamic arrays which means it can be extended as much as we want. We can insert the elements at the end and elements are stored in continuous memory locations which can be accessed using iterators as we iterate over the array of elements.

In order to use vectors in our code we have to include a vector standard library. We can create a vector using the following syntax :

vector<data_type> vector_name;

Here data_type means the type of elements we want to store in the vector. While creating the vector itself we can put some elements into it like below or else we can create a vector like the above and insert elements into it using push_back() in a for a loop. In further discussion let’s use the simpler approach like below.

vector<data_type> vector_name{elements};

Generally, to copy elements of one array to another we have only a single approach that is simply assigning each element of one array to another using a loop. But, for vectors, we have many ways to copy elements. Let’s discuss them with examples.

Different ways to copy elements of one vector to another in C++

Approach 1 : 

Here we simply create a vector “v1” with some elements and create another vector “v2” in which we want we copy elements of “v1”, then simply iterate over “v1” and push the elements into “v2” using push_back() inbuilt method of the vector.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    
    vector<int> v1{5,2,3};
    vector<int> v2;
    for(int i=0;i<v1.size();i++)
    {
        v2.push_back(v1[i]);
    }
    cout<<"v1 vector elements : "; 
    for(int i=0;i<v1.size();i++)
    {
        cout<<v1[i]<<" ";
    }
    cout<<"\n";
    cout<<"v2 vector elements : "; 
    for(int i=0;i<v2.size();i++)
    {
        cout<<v2[i]<<" ";
    }
    return 0;
}

Output :

v1 vector elements : 5 2 3 
v2 vector elements : 5 2 3

In the above code if we try to change the elements of vector v1 then there will be no change in vector v2 elements because v2 is just a copy of v1 and they do not point to the same address location.

Approach 2 :

In this approach, we created a vector “v1” with some elements in it and create a vector “v2” and passed v1 as a parameter of a constructor which makes a deep copy of v1 into v2.To understand it clearly look at the below code.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    
    vector<int> v1{5,2,3};
    vector<int> v2(v1);
    cout<<"v1 vector elements : "; 
    for(int i=0;i<v1.size();i++)
    {
        cout<<v1[i]<<" ";
    }
    cout<<"\n";
    cout<<"v2 vector elements : "; 
    for(int i=0;i<v2.size();i++)
    {
        cout<<v2[i]<<" ";
    }
    return 0;
}

Output : 

v1 vector elements : 5 2 3 
v2 vector elements : 5 2 3

Approach 3 : 

In this approach, we will create a vector v1 with some elements and we create a vector v2, assigning v1 to v2 which leads to deep copy which means they do not possess the same address or memory location.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    
    vector<int> v1{5,2,3};
    vector<int> v2;
    v2=v1;
    cout<<"v1 vector elements : "; 
    for(int i=0;i<v1.size();i++)
    {
        cout<<v1[i]<<" ";
    }
    cout<<"\n";
    cout<<"v2 vector elements : "; 
    for(int i=0;i<v2.size();i++)
    {
        cout<<v2[i]<<" ";
    }
    return 0;
}

Output :

v1 vector elements : 5 2 3 
v2 vector elements : 5 2 3

Among all the approaches used to copy elements of a vector into another, approaches 2 and 3 seem to be the easy way because of the simple code.

Till now we haven’t used any inbuilt functions to copy a vector to another. From now we are going to use some standard library functions like assign, copy, insert, etc.

Using Some inbuilt functions

Approach 4 :

In this approach, we are going to use the assign method in the vector header file. In this function, we are going to pass two parameters.

Syntax : 

copyvector_name.assign(v1_iterator_starting,v1_iterator_ending);

Here copyvector_name means v2, into which we are going to copy the elements of v1.

Parameters : 

v1_iterator_starting means v1.begin(), similarly v1_iterator_ending means v1.begin().Where v1 is the original vector.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    
    vector<int> v1{5,2,3};
    vector<int> v2;
    v2.assign(v1.begin(), v1.end());
    cout<<"v1 vector elements : "; 
    for(int i=0;i<v1.size();i++)
    {
        cout<<v1[i]<<" ";
    }
    cout<<"\n";
    cout<<"v2 vector elements : "; 
    for(int i=0;i<v2.size();i++)
    {
        cout<<v2[i]<<" ";
    }
    return 0;
}

Output : 

v1 vector elements : 5 2 3 
v2 vector elements : 5 2 3

Approach 5 : 

copy() inbuilt function in C++ is used to copy one vector to another. This function takes three parameters.

Syntax : 

copy(v1_iterator_starting, v1_iterator_ending, back_inserter(copyvector_name) );

Here back_inserter is an iterator designed to insert the elements into the container to which it is applied and here copyvector_name is v2.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    
    vector<int> v1{5,2,3};
    vector<int> v2;
    copy(v1.begin(), v1.end(), back_inserter(v2));
    cout<<"v1 vector elements : "; 
    for(int i=0;i<v1.size();i++)
    {
        cout<<v1[i]<<" ";
    }
    cout<<"\n";
    cout<<"v2 vector elements : "; 
    for(int i=0;i<v2.size();i++)
    {
        cout<<v2[i]<<" ";
    }
    return 0;
}

Output : 

v1 vector elements : 5 2 3 
v2 vector elements : 5 2 3

There is another approach using the insert function in C++ standard library. All these approaches we have discussed are used to deep copy. Hope you have clearly understood the entire discussion.

Leave a Reply

Your email address will not be published. Required fields are marked *