Ways to Copy a Vector in C++



In C++, vectors are commonly used to store dynamic collections of data. Sometimes, we need to copy a vector to manipulate data without affecting the original. In this article, we'll show you the easiest ways to copy a vector in C++, along with examples to show you how it works.

Ways to copy a vector in C++

There are different ways to copy a vector in C++, and each method has its own use. We'll cover the following approaches:

Using std::copy

std::copy is a built-in function that copies elements from one vector to another. It is commonly used to copy elements into an existing vector.

Syntax

Here's the syntax for using std::copy to copy elements from one vector to another:

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

Following is the pseudocode for using std::copy:

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

Here's an example that shows how to use std::copy to copy elements from v1 to v2 using the copy() function with back_inserter to add elements to the end of v2.

#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

Below is the output of the above program:

v1 vector elements are : 7 6 4 5
v2 vector elements are : 7 6 4 5

Using assign() Method

The assign() method copies a range of elements from one vector to another, and it's commonly used to replace the contents of the target vector.

Syntax

Here's the syntax for using std::assign to copy elements:

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

Below is the pseudocode that shows how to use the assign() method.

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

Here's the example code that shows how to use the assign() method to copy elements from vector v1 to vector v2, and then print both vectors to verify the copy.

#include<iostream> // 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

Below is the output of the above program:

v1 vector elements are : 7 6 4 5 
v2 vector elements are : 7 6 4 5

By assignment "=" operator

Using the assignment operator (=) directly copies all the elements from one vector to another. This is the easiest way to copy a vector in C++.

Algorithm

Below is the pseudocode that describes how the assignment operator is used to copy elements between vectors:

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

In this example, we show how the assignment operator copies elements from vector v1 to vector v2. It then prints the elements of both vectors to confirm the copy was successful.

#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

Below is the output of the above program:

v1 vector elements are : 7 6 4 5
v2 vector elements are : 7 6 4 5

By push_back method

You can use a loop with push_back() to copy elements from one vector to another. This method is slower but offers more flexibility in handling elements.

Algorithm

Below is the pseudocode for copying elements from one vector to another using the push_back() method:

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

Here's the example code that shows how to copy elements from vector v1 to vector v2 using a loop with the push_back() method.

#include<iostream> // 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

Below is the output of the above program:

v1 vector elements are : 7 6 4 5
v2 vector elements are : 7 6 4 5

By passing vector as constructor

You can create a copy of an existing vector using its constructor. This method makes a deep copy of the original vector into a new one.

Algorithm

Below is the pseudocode showing how to create a copy of a vector using the constructor:

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

This example shows how to create a copy of a vector using the constructor. We initialize v1 with elements and create v2 by passing v1 to the constructor. Both vectors' elements are printed.

#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

Below is the output of the above program:

v1 vector elements are : 7 6 4 5
v2 vector elements are : 7 6 4 5

Conclusion

In this article, we've covered different ways to copy a vector in C++, including methods like assign(), std::copy, the assignment operator, and more. Knowing when to use each method will help you work with vectors more easily in your code.

Updated on: 2025-01-30T14:45:41+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements