0
0
CppHow-ToBeginner · 4 min read

How to Copy Vector in C++: Simple Methods Explained

To copy a std::vector in C++, you can use the assignment operator or the copy constructor, both of which create a new vector with the same elements. Alternatively, you can use std::copy from the <algorithm> header to copy elements manually.
📐

Syntax

Here are the common ways to copy a std::vector in C++:

  • Assignment operator: vector2 = vector1; copies all elements from vector1 to vector2.
  • Copy constructor: std::vector<T> vector2(vector1); creates vector2 as a copy of vector1.
  • std::copy: Use std::copy(vector1.begin(), vector1.end(), vector2.begin()); to copy elements manually (requires vector2 to have enough space).
cpp
std::vector<int> vector1 = {1, 2, 3};

// Using assignment operator
std::vector<int> vector2;
vector2 = vector1;

// Using copy constructor
std::vector<int> vector3(vector1);

// Using std::copy
std::vector<int> vector4(vector1.size());
std::copy(vector1.begin(), vector1.end(), vector4.begin());
💻

Example

This example shows how to copy a vector using the assignment operator, copy constructor, and std::copy. It prints the copied vectors to confirm they contain the same elements.

cpp
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> original = {10, 20, 30};

    // Copy using assignment operator
    std::vector<int> copy1;
    copy1 = original;

    // Copy using copy constructor
    std::vector<int> copy2(original);

    // Copy using std::copy
    std::vector<int> copy3(original.size());
    std::copy(original.begin(), original.end(), copy3.begin());

    // Print all vectors
    std::cout << "copy1: ";
    for (int n : copy1) std::cout << n << ' ';
    std::cout << "\n";

    std::cout << "copy2: ";
    for (int n : copy2) std::cout << n << ' ';
    std::cout << "\n";

    std::cout << "copy3: ";
    for (int n : copy3) std::cout << n << ' ';
    std::cout << "\n";

    return 0;
}
Output
copy1: 10 20 30 copy2: 10 20 30 copy3: 10 20 30
⚠️

Common Pitfalls

Common mistakes when copying vectors include:

  • Using std::copy without resizing the destination vector first, which causes undefined behavior.
  • Confusing shallow copy with deep copy — vectors copy their elements, so the new vector is independent.
  • Trying to copy pointers inside vectors without managing the pointed-to data (not related to vector copying itself).
cpp
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> source = {1, 2, 3};
    std::vector<int> dest; // empty vector

    // WRONG: dest has no space, std::copy causes undefined behavior
    // std::copy(source.begin(), source.end(), dest.begin());

    // CORRECT: resize dest first
    dest.resize(source.size());
    std::copy(source.begin(), source.end(), dest.begin());

    return 0;
}
📊

Quick Reference

Summary of vector copy methods:

MethodDescriptionNotes
Assignment operatorCopies all elements to existing vectorDestination vector is replaced
Copy constructorCreates new vector as copyUsed at initialization
std::copyCopies elements manuallyDestination must have enough space

Key Takeaways

Use assignment operator or copy constructor for simple and safe vector copying.
When using std::copy, always resize the destination vector before copying.
Copied vectors are independent; changes to one do not affect the other.
Avoid copying pointers inside vectors without managing the pointed data carefully.
Copy constructor is useful when creating a new vector initialized from another.