×

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

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

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.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 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++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

Counting Frequencies of Array Elements in C++

We have an array of integer items with duplicate values, and our objective is to compute the frequencies of the different elements in the array. Methods: Methods that can be used to...

3 minutes read.

User-defined literals in C++

Introduction to User-Defined Literals: User-defined literals, introduced in C++11, are a way to extend the C++ language to allow users to define their literal suffixes and the corresponding behavior. These suffixes...

7 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

C++ Bitset

Overview In C++, bitset represents a fixed-sequence of some bits values by either 0 and 1. Zero represents the value as false or unset, while 1 represents the value as true...

4 minutes read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 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.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

Lexicographically Next Permutation in C++

In this tutorial, we'll look at how to use C++ to generate the lexicographically next permutation of a string. The lexicographically next permutation is the larger permutation. "ACB," for example,...

3 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.

Top 5 IDEs for C++ That You Should Try Once

In the past decades, creating an application or interface from the very basic idea, the developer has to struggle a lot for it. Because an application is a combination of...

3 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 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.