Copy one vector’s elements to another (Simple approach)
Syntax
std::copy(first_iterator_o, last_iterator_o, back_inserter()):
first_iteratot_0 = First iterator of first vector
last_iteratot_0 = Last iterator of first vector
back_inserter() = To insert values from back
Algorithm
Begin
Declare v1 of vector type.
Initialise some values into v1 vector in the array pattern.
Declare v2 of vector type.
Call copy(v1.begin(), v1.end(), back_inserter(v2)) to copy all
elements of v1 to v2.
Print “v1 vector elements are:”.
for (int i=0;i<1.size; i++)
print the all element of v2 vector.
Print “v2 vector elements are :”.
for (int i=0;i<2.size; i++)
print the all element of v2 vector.
End.
include<iostream>
blog banner 1
include<vector>
using namespace std;
int main()
{
//declar and initialise vector 1
vector v1{10,20,30,40,50};
//declare vector2
vector v2;
//copy v2 to v1
for(int i=0; i<v1.size(); i++){
v2.push_back(v1[i]);
}
//printing v1 and v2
cout<<"v1 elements: ";
for(int i=0; i<v1.size(); i++){
cout<<v1[i]<<" ";
}
cout<<endl;
cout<<“v2 elements: “;
for(int i=0; i<v2.size(); i++){
cout<<v2[i]<<” “;
}
cout<<endl;
return 0;
}
Output
v1 elements: 10 20 30 40 50
v2 elements: 10 20 30 40 50
Copy vector by using an assignment operator
Syntax
std::assign(first_iterator_o, last_iterator_o):
first_iteratot_0 = First iterator of first vector.
last_iteratot_0 = Last iterator of first vector.
Algorithm
Begin
Initialise a vector v1 with its elements.
Declare another vector v2.
Call assign() to copy the elements of v1 to v2.
Print the elements of v1.
Print the elements of v2.
End.
include<iostream>
include<vector>
using namespace std;
int main()
{
//declar and initialise vector 1
vector v1{10,20,30,40,50};
//declare vector2
vector v2;
//copying v1 to v2
v2 = v1;
//printing v1 and v2
cout<<"v1 elements: ";
for(int i=0; i<v1.size(); i++){
cout<<v1[i]<<" ";
}
cout<<endl;
cout<<"v2 elements: ";
for(int i=0; i<v2.size(); i++){
cout<<v2[i]<<" ";
}
cout<<endl;
return 0;
//printing v1 and v2
cout<<“v1 elements: “;
for(int i=0; i<v1.size(); i++){
cout<<v1[i]<<” “;
}
cout<<endl;
cout<<“v2 elements: “;
for(int i=0; i<v2.size(); i++){
cout<<v2[i]<<” “;
}
cout<<endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output
v1 elements: 10 20 30 40 50
v2 elements: 10 20 30 40 50
By push_back method
Algorithm
Begin
Initialise a vector v1 with its elements.
Declare another vector v2.
Make a for loop to copy elements of the first vector into the second vector by Iterative method using push_back().
Print the elements of v1.
Print the elements of v2.
End.
Example Code
include #include // for vector
include
include// for vector
include// for copy() and assign()
include// for back_inserter
using namespace std;
int main() {
vector v1{7,6,4,5};
vector v2;
for (int i=0; i<v1.size(); i++)
v2.push_back(v1[i]);
cout << “v1 vector elements are : “;
for (int i=0; i<v1.size(); i++)
cout << v1[i] << ” “;
cout << endl;
cout << “v2 vector elements are : “;
for (int i=0; i<v2.size(); i++)
cout << v2[i] << ” “;
cout<< endl;
return 0;
}
Output
v1 vector elements are : 7 6 4 5
v2 vector elements are : 7 6 4 5
Copy vector 1 to vector 2 while declaring vector 2 by passing the first vector as an argument (parameterised constructor)
Syntax:
vector v2(v1);
include<iostream>
include<vector>
using namespace std;
int main()
{
//declar and initialise vector 1
vector v1{10,20,30,40,50};
//declare vector2 by copying vector1
vector v2(v1);
//printing v1 and v2
cout<<"v1 elements: ";
for(int i=0; i<v1.size(); i++){
cout<<v1[i]<<" ";
}
cout<<endl;
cout<<"v2 elements: ";
for(int i=0; i<v2.size(); i++){
cout<<v2[i]<<" ";
}
cout<<endl;
return 0;
}
Output
v1 elements: 10 20 30 40 50
v2 elements: 10 20 30 40 50
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 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.
// C++ code to demonstrate copy of vector
// by assign() and copy().
include
include // for vector
include // for copy() and assign()
include // for back_inserter
using namespace std;
int main()
{
// Initialising vector with values
vector 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;
}

You can also try this code with Online C++ Compiler
Run Code
// C++ code to demonstrate copy of vector
// by assign() and copy().
include
include // for vector
include // for copy() and assign()
include // for back_inserter
using namespace std;
int main()
{
// Initialising vector with values
vector 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 the old vector is:2
The first element of the new vector is:1

You can also try this code with Online C++ Compiler
Run Code
-
assign(first_iterator_o, last_iterator_o) :- This method assigns the same values to new vector as old one. This takes 2 arguments, the first iterator to old vector and last iterator to the 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_inserter
using namespace std;
int main()
{
// Initialising vector with values
vector 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 the old vector is:2
The first element of the new vector is:1
Check out this problem - Shortest Common Supersequence.
Conclusion
Looking to take up a course? Well, Coding Ninjas is just the right place for you.
To learn more about how to build your career in programming, click here.
Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; look at the Top 150 Interview Puzzles, interview experiences, and interview bundle for placement preparations. Read our blogs on aptitude, competitive programming, interview questions, IT certifications, and data structures and algorithms for the best practice.