Ways to Copy a Vector in C++
Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which we have in Arrays. There are mainly six different ways to copy a vector in C++they are discussed below in detail.
1. Iterative Method
Below is the code to demonstrate the copy of the vector by using the assignment-operator method
Code
// C++ code to demonstrate copy of vector
// by an iterative method.
#include<iostream>
#include<vector>
using namespace std;
int main() {
// Initializing vector with values
vector<int> vect1{221, 1112, 3232223, 22224};
vector<int> vect2;
//assignment operator
vect2 = vect1;
cout << "1st vector elements are : ";
for (int i=0; i<vect1.size(); i++)
cout << vect1[i] << " ";
cout << endl;
cout << "present vector elements are : ";
for (int i=0; i<vect2.size(); i++)
cout << vect2[i] << " ";
cout<< endl;
// copy has been created.
vect1[0] = 2;
cout << "this is the first element of old vector:";
cout << vect1[0] << endl;
cout << "this is the first element of the new vector is:";
cout << vect2[0] <<endl;
return 0; }
Output
1st vector elements are: 221 1112 3232223 22224
present vector elements are : 221 1112 3232223 22224
this is the first element of the old vector:2
this is the first element of the new vector:221
2. By Adapting assignment=operator
Below is the code to demonstrate the working of Vectors in C++ programming language and also to copy the vector
Code
//Here, we have written the C++ code to demonstrate a copy of the //vector
// by adapting assignment=operator
#include<iostream>
#include<vector>
using namespace std;
int main() {
//here, we are initializing a vector with values
vector<int> vect1{1, 2, 3, 4};
//here we are declaring a new vector
vector<int> vect2;
for (int i=0; i<vect1.size(); i++)
vect2.push_back(vect1[i]);
cout << "the old vector elements are : ";
for (int i=0; i<vect1.size(); i++)
cout << vect1[i] << " ";
cout << endl;
cout << "the new vector elements are : ";
for (int i=0; i<vect2.size(); i++)
cout << vect2[i] << " ";
cout<< endl;
vect1[0] = 2;
cout << "first element of old vector is :";
cout << vect1[0] << endl;
cout << "first element of new vector is :";
cout << vect2[0] <<endl;
return 0;
}
Output
the old vector elements are: 1 2 3 4
the new vector elements are: 1 2 3 4
the first element of the old vector is:2
the first element of the new vector is :1
3. By insert() Method
Below is the code to demonstrate the working of C++ vectors and the copy function by the insert() function method
Code
//Here, we have written the C++ code to demonstrate a copy of the //vector
// by adapting insert() function
#include<iostream>
#include<vector> // for vector
using namespace std;
int main(){
//here, we are initializing a vector with values
vector<int> vect1{111111, 3212, 23, 422};
//here we are declaring a new vector
vector<int> vect2;
//here, we are copying vector by inserting 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 << "the new vector elements : ";
for (int i=0; i<vect2.size(); i++)
cout << vect2[i] << " ";
cout<< endl;
// copy has been created.
vect1[0] = 2;
cout << "first element of old vector :";
cout << vect1[0] << endl;
cout << "This is the first element of the new vector is:";
cout << vect2[0] <<endl;
return 0;
}
Output
Old vector elements are: 111111 3212 23 422
the new vector elements : 111111 3212 23 422
the first element of old vector:2
This is the first element of the new vector:111111
4. By assign() Operator
Below is the code to demonstrate the working of C++ vectors and the copy function by assign() operator
Code
//Here, we have written the C++ code to demonstrate a copy of the //vector
// by assign operator()
#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
int main() {
//here, we are initializing a vector with values
vector<int> vect1{11212, 21212, 2223, 2222224};
vector<int> vect2;
//assign function
vect2.assign(vect1.begin(), vect1.end());
cout << "the old vector elements : ";
for (int i=0; i<vect1.size(); i++)
cout << vect1[i] << " ";
cout << endl;
cout << "the new vector elements : ";
for (int i=0; i<vect2.size(); i++)
cout << vect2[i] << " ";
cout<< endl;
//the copy has been created.
vect1[0] = 2;
cout << "first element of old vector :";
cout << vect1[0] << endl;
cout << "first element of new vector :";
cout << vect2[0] <<endl;
return 0;
}
Output
the old vector elements: 11212 21212 2223 2222224
the new vector elements : 11212 21212 2223 2222224
the first element of old vector:2
the first element of new vector:11212
5. By assign() and copy() Operator
Below is the code to demonstrate the working of C++ vectors and the copy function by assign() and copy() operator
Code
//Here, we have written the C++ code to demonstrate a copy of the //vector
// by assign operator() and copy() operator
#include<iostream>
#include<vector> //vector
#include<algorithm> //copy() and assign()
#include<iterator>
using namespace std;
int main() {
vector<int> vect1{1111, 232, 223, 2224};
vector<int> vect2;
copy(vect1.begin(), vect1.end(), back_inserter(vect2));
cout << "the old vector elements : ";
for (int i=0; i<vect1.size(); i++)
cout << vect1[i] << " ";
cout << endl;
cout << "the new vector elements : ";
for (int i=0; i<vect2.size(); i++)
cout << vect2[i] << " ";
cout<< endl;
//copy has been
vect1[0] = 2;
cout << "first element of old vector :";
cout << vect1[0] << endl;
cout << "first element of new vector :";
cout << vect2[0] <<endl;
return 0;
}
Output
the old vector elements: 1111 232 223 2224
the new vector elements : 1111 232 223 2224
the first element of old vector:2
the first element of new vector:1111
6. By Constructor Method
Below is the code to demonstrate the working of C++ vectors and the copy function by constructor method
Code
//Here, we have written the C++ code to demonstrate a copy of the //vector
// by constructor method
#include<bits/stdc++.h>
using namespace std;
int main() {
//here, we are initializing a vector with values
vector<int> vect1{1111, 3332, 56783, 13344};
// constructor method, Deep copy
vector<int> vect2(vect1);
cout << "the old vector elements are : ";
for (int i=0; i<vect1.size(); i++)
cout << vect1[i] << " ";
cout << endl;
cout << "the new vector elements are : ";
for (int i=0; i<vect2.size(); i++)
cout << vect2[i] << " ";
cout<< endl;
// copy has been created.
vect1[0] = 2;
cout << "first element of old vector :";
cout << vect1[0] << endl;
cout << "first element of new vector :";
cout << vect2[0] <<endl;
return 0;
}
Output
the old vector elements are: 1111 3332 56783 13344
the new vector elements are: 1111 3332 56783 13344
the first element of old vector:2
the first element of new vector:1111