0
0
CppHow-ToBeginner · 2 min read

C++ How to Convert Vector to Array with Example

You can convert a std::vector to a plain array by creating a new array and copying elements using std::copy or a loop, for example: int arr[size]; std::copy(vec.begin(), vec.end(), arr);.
📋

Examples

Input[1, 2, 3]
Output[1, 2, 3]
Input[10, 20, 30, 40]
Output[10, 20, 30, 40]
Input[]
Output[]
🧠

How to Think About It

To convert a vector to an array, first create an array with the same size as the vector. Then copy each element from the vector into the array. This works because vectors store elements contiguously, so copying is straightforward.
📐

Algorithm

1
Get the size of the vector.
2
Create an array with the same size.
3
Copy each element from the vector to the array.
4
Use the array as needed.
💻

Code

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

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    int size = vec.size();
    int arr[5];
    std::copy(vec.begin(), vec.end(), arr);

    for (int i = 0; i < size; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}
Output
1 2 3 4 5
🔍

Dry Run

Let's trace converting vector {1, 2, 3, 4, 5} to array.

1

Get vector size

size = 5

2

Create array

int arr[5];

3

Copy elements

arr = {1, 2, 3, 4, 5}

IndexVector ElementArray Element
011
122
233
344
455
💡

Why This Works

Step 1: Create array with vector size

We create a plain array with the same number of elements as the vector using int arr[size];.

Step 2: Copy elements

Using std::copy, we copy each element from the vector's start to end into the array.

Step 3: Use the array

Now the array holds the same values as the vector and can be used like any normal C++ array.

🔄

Alternative Approaches

Manual loop copy
cpp
#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3};
    int size = vec.size();
    int arr[3];
    for (int i = 0; i < size; ++i) {
        arr[i] = vec[i];
    }
    for (int i = 0; i < size; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}
This method uses a simple loop instead of std::copy, which is easy to understand but slightly more verbose.
Using vector's data() pointer
cpp
#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {5, 6, 7};
    int* arr = vec.data();
    for (int i = 0; i < vec.size(); ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}
This does not create a new array but uses the vector's internal array pointer directly; changes to arr affect vec.

Complexity: O(n) time, O(n) space

Time Complexity

Copying each element from the vector to the array takes linear time proportional to the number of elements.

Space Complexity

A new array of the same size as the vector is created, so space usage is linear.

Which Approach is Fastest?

Using std::copy or a manual loop both have similar performance; using data() is fastest but shares memory.

ApproachTimeSpaceBest For
std::copyO(n)O(n)Safe and clear copying
Manual loopO(n)O(n)Simple and explicit copying
Using data()O(1)O(0)When no copy needed and vector lifetime is guaranteed
💡
Use std::copy or a loop to copy vector elements into a fixed-size array safely.
⚠️
Trying to assign a vector directly to an array without copying causes errors because arrays and vectors are different types.