×

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

Timsort Implementation Using C++

Timsort Implementation Using C++ The Timsort is a stable sorting algorithm that uses the idea of merge sort and insertion sort. It can also be called a hybrid algorithm of insertion...

3 minutes read.

Check for Balanced Brackets in an Expression (well-formedness) using Stack

Write a program that check the correctness of the pairs and ordering of the characters “{“, “}”, “(“, “)”, “[“, “]” in the expression string exp. Example: Checking for balanced parenthesis is one of...

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

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example, The array number 147 is 1,4,7. To add...

3 minutes read.

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance...

3 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

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

Program to find the GCD of two numbers in C++

Before understanding the program of GCD or HCF, one must know what GCD or HCF is. What is GCD? The GCD is referred to as Greatest Common Divisor. HCF is the other...

8 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

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

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

Difference between C and C++

What do you mean by C? C is a machine-independent structure or procedural oriented computer language that is widely utilized in a variety of applications. C is a fundamental programming language...

4 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.