The Wayback Machine - https://web.archive.org/web/20250114144521/https://www.geeksforgeeks.org/ways-copy-vector-c/
Open In App

Different Ways to Copy a Vector in C++

Last Updated : 20 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Copying a vector is the process of creating a new vector that contains same elements as original vector. In this article, we will learn different ways to copy a vector in C++.

The most efficient and easiest method to copy a vector in C++ is by using assignment operator (=).  Let’s take a look at an example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {2, 4, 1, 5, 3};

    // Assigning the vector v1 to vector v2
    vector<int> v2 = v1;

    for (auto i : v2)
        cout << i << " ";
    return 0;
}

Output
2 4 1 5 3 

C++ also provides more methods to copy a vector into another vector. They are as follows:

Using Vector Constructor

Another easy way to copy a vector at the time of declaration is by passing the original vector to the copy constructor of another vector.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {2, 4, 1, 5, 3};

    // Copying vector v1 into vector v2
    vector<int> v2(v1);

    for (auto i : v2)
        cout << i << " ";
    return 0;
}

Output
2 4 1 5 3 

Using copy()

The copy() function is our go-to method to copy data in C++. It can copy one vector to another using their iterators. Moreover, the copy_if() variant of this function can also perform selective copy on the basis of a unary predicate function.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {2, 4, 1, 5, 3};
    vector<int> v2;

    // Copying vector v1 into vector v2
    copy(v1.begin(), v1.end(), back_inserter(v2));

    for (auto i : v2)
        cout << i << " ";
    return 0;
}

Output
2 4 1 5 3 

Explanation: We have to use back_insertor() function if we haven’t specified the vector size during declaration. If we have already had enough space, then we have to pass v2.begin() instead.

Using Vector assign()

The vector assign() method is equivalent to the = operator method but it needs the iterator to the vector to be copied.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {2, 4, 1, 5, 3};
    vector<int> v2;

    // Copying the vector v1 into vector v
    v2.assign(v1.begin(), v1.end());

    for (auto i : v2)
        cout << i << " ";
    return 0;
}

Output
2 4 1 5 3 

Using Vector insert()

The vector insert() method provides a version that can copy all the elements from the given range. This can be used to copy the entire vector to another in a similar way as assign() method.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {2, 4, 1, 5, 3};
    vector<int> v2;

    // Copying vector v1 into vector v2
    v2.insert(v2.begin(), v1.begin(), v1.end());

    for (auto i : v2)
        cout << i << " ";
    return 0;
}

Output
2 4 1 5 3 

Using Vector push_back()

Push all the elements of the first vector into the second vector by using vector push_back() method. A loop can be to iterate through the first vector.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {2, 4, 1, 5, 3};
    vector<int> v2;

    // Copying vector v1 into vector v2
    for (auto i : v1)
        v2.push_back(i);

    for (auto i : v2)
        cout << i << " ";
    return 0;
}

Output
2 4 1 5 3 

This method provides more control as we can also modify the element if needed before copying.

Frequently Asked Questions – FAQs

Which method is the most efficient to copy a vector?

Almost all the methods are equally efficient but the assignment operator or copy constructor methods are usually the easiest to use.

Can I copy only part of a vector?

Yes, we can use any method that uses iterator to copy only a part of the vector.

What is time complexity to copy a vector?

The time complexity to copy a vector is O(n), where n is the number of elements.


Master C++ with our comprehensive C++ Programming Course Online, perfect for anyone from beginners to advanced learners. Learn everything from the fundamentals to advanced C++ concepts, and apply your knowledge through practical exercises.

Take the Three 90 Challenge! Finish 90% of the course in 90 days, and enjoy a 90% refund. Stay focused, track your progress, and challenge yourself to complete the course! Join today and begin your C++ programming journey!


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg