
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Ways to copy a vector in C++
There are different ways to copy a vector in C++.
1) std::copy
std:: copy is inbuilt to copy the elements from one vector to another.
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. Initialize some values into v1 vector in 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.
Example Code
#include<iostream> #include<vector> #include<algorithm> // for copy. #include<iterator> // for back_inserter using namespace std; int main() { vector<int> v1{ 7, 6, 4, 5 }; vector<int> v2; copy(v1.begin(), v1.end(), back_inserter(v2)); 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
2) assign operator
This is also used to copy values from vector 1 to vector 2.
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 Initialize 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.
Example Code
#include<iostream> #include<vector> // for vector #include<iostream> #include<vector>// for vector #include<algorithm>// for copy() and assign() #include<iterator>// for back_inserter using namespace std; int main() { vector<int> v1{7,6,4,5}; vector<int> v2; v2.assign(v1.begin(), v1.end()); 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
3) By assignment “=” operator
It is a simple way to copy values from vector 1 to vector 2
Algorithm
Begin Initialize a vector v1 with its elements. Declare another vector v2. Call assignment operator “=” to copy the elements of v1 to v2. Print the elements of v1. Print the elements of v2. End.
Example Code
#include<iostream> #include<vector> // for vector #include<algorithm>// for copy() and assign() #include<iterator>// for back_inserter using namespace std; int main() { vector<int> v1{7,6,4,5}; vector<int> v2; v2 = v1 ; 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
4) By push_back method
Algorithm
Begin Initialize a vector v1 with its elements. Declare another vector v2. Make a for loop to copy elements of first vector into second vector by Iterative method using push_back(). Print the elements of v1. Print the elements of v2. End.
Example Code
#include<iostream> #include<vector> // for vector #include<iostream> #include<vector>// for vector #include<algorithm>// for copy() and assign() #include<iterator>// for back_inserter using namespace std; int main() { vector<int> v1{7,6,4,5}; vector<int> 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
5) By passing vector as constructor
Algorithm
Begin Initialize a vector v1 with its elements. Declare another vector v2 and copying elements of first vector to second vector using constructor method and they are deeply copied. Print the elements of v1. Print the elements of v2. End.
Example Code
#include<iostream> #include<vector>// for vector #include<algorithm>// for copy() and assign() #include<iterator>// for back_inserter using namespace std; int main() { vector<int> v1{7,6,4,5}; vector<int> v2(v1); 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
- Related Articles
- Ways to copy dictionary in python
- Python - Ways to Copy Dictionary
- How to copy elements of ArrayList to Java Vector
- How to append a vector in a vector in C++?
- How to convert a string vector into an integer vector in R?
- How to generate a normal random vector using the mean of a vector in R?
- How to multiply each element of a larger vector with a smaller vector in R?
- How to replace values in a vector with values in the same vector in R?
- How to initialize a vector in C++?
- Passing a vector to constructor in C++
- How to reverse a vector in R?
- Add elements to a Vector in Java
- How to copy multiple files in PowerShell using Copy-Item?
- Check if it is possible to reach vector B by rotating vector A and adding vector C to its in Python
- R programming to subtract all values in a vector from all values in another vector.

Advertisements