×

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++ STL (Standard Template Library)

Introduction C++ is a flexible type and general proposed programming language. So we need a standard library that supports C++. C++ STL (Standard Template Library) is a collection of templates that...

6 minutes read.

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer...

3 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

C++ First Program

Let's write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler. Open any editor to write C++ program. #include<iostream>   using namespace std;   int main(){       cout<<"Welcome to C++ program"<<endl;   } Output...

2 minutes read.

Queue in C++

What is Queue? As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is...

4 minutes read.

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

Function Pointer in C++

Definition: A function pointer in C++ is the same as a usual pointer which is used to point some variables. Function pointers are used to point functions or say store the...

4 minutes read.

How to implement map in C++

Part of the C++ STL is maps (Standard Template Library). Maps are associative containers that hold sorted key-value pairs, where each key is distinct and may only be added or...

4 minutes read.

Sliding Window Technique in C++

Sliding Window Technique or Window Sliding Technique is a computational technique that is mainly used to reduce the use of nested loop and replace it with a single loop. It...

4 minutes read.

What does Buffer Flush mean in C++

A buffer flush, to explain simple layman's terms, is nothing but the transfer of computer data which is being stored in a rentable temporary memory of your computer running either...

3 minutes read.

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

Hospital Management Project in C++

The following capabilities are required to build a hospital management project: These are as follows: hospitals' names, contact information, and lists of doctors and patients. Activities Supported Hospital Data Print Patients' data to...

4 minutes read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.