×

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

Related Topics

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Learn C++ Tutorial

C++ Introduction C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories. It is superset (extension) of C programming language. Depending upon features supported by programming...

10 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

2 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

std::min in C++

std::min in C++ std::min is specified in the program code, used to calculate the lowest amount that has been transferred. When there's more of someone who returns first of them. It's used...

2 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 minutes read.

SDL library in C++ with Examples

SDL stands for Simple DirectMedia Layer. Using OpenGL and Direct3D, it is a cross-platform development library created to give users low-level access to audio, keyboard, mouse, joystick, and graphics hardware....

3 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.