0
0
CppHow-ToBeginner · 2 min read

C++ How to Convert Array to Vector Easily

You can convert an array to a vector in C++ by using the vector constructor like std::vector v(arr, arr + size);, where arr is your array and size is its length.
📋

Examples

Inputint arr[] = {1, 2, 3}; int size = 3;
Outputvector contains [1, 2, 3]
Inputint arr[] = {10, 20, 30, 40}; int size = 4;
Outputvector contains [10, 20, 30, 40]
Inputint arr[] = {}; int size = 0;
Outputvector is empty []
🧠

How to Think About It

To convert an array to a vector, think of the array as a fixed list of items and the vector as a flexible list. You create the vector by telling it where the array starts and where it ends, so it copies all elements inside that range.
📐

Algorithm

1
Identify the array and its size.
2
Use the vector constructor that takes two pointers or iterators: one to the start of the array and one past the last element.
3
Create the vector using these pointers to copy the array elements.
💻

Code

cpp
#include <iostream>
#include <vector>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    std::vector<int> v(arr, arr + size);

    for (int num : v) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    return 0;
}
Output
1 2 3 4 5
🔍

Dry Run

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

1

Calculate size

size = 5 because array has 5 elements

2

Create vector

vector v copies elements from arr[0] to arr[5] (exclusive)

3

Print vector

Output elements: 1 2 3 4 5

StepActionValue
1size calculation5
2vector creationv = {1, 2, 3, 4, 5}
3print vector1 2 3 4 5
💡

Why This Works

Step 1: Array to pointer conversion

The array name arr acts like a pointer to its first element.

Step 2: Vector constructor with iterators

The vector constructor takes two pointers: start and end, copying all elements in between.

Step 3: Resulting vector

The vector now contains all elements from the array and can be used like a dynamic list.

🔄

Alternative Approaches

Using std::copy and std::back_inserter
cpp
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    int arr[] = {1, 2, 3};
    int size = sizeof(arr) / sizeof(arr[0]);
    std::vector<int> v;
    std::copy(arr, arr + size, std::back_inserter(v));
    for (int num : v) std::cout << num << " ";
    std::cout << std::endl;
    return 0;
}
This method copies elements one by one and allows appending to an existing vector.
Using a loop to push_back elements
cpp
#include <iostream>
#include <vector>

int main() {
    int arr[] = {4, 5, 6};
    int size = sizeof(arr) / sizeof(arr[0]);
    std::vector<int> v;
    for (int i = 0; i < size; ++i) {
        v.push_back(arr[i]);
    }
    for (int num : v) std::cout << num << " ";
    std::cout << std::endl;
    return 0;
}
This is the most manual way but very clear for beginners.

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

Time Complexity

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

Space Complexity

The vector allocates new memory to store all elements, so space is proportional to the array size.

Which Approach is Fastest?

Using the vector constructor with pointers is fastest and simplest; manual loops or std::copy add overhead.

ApproachTimeSpaceBest For
Vector constructor with pointersO(n)O(n)Quick and direct conversion
std::copy with back_inserterO(n)O(n)Appending to existing vector
Manual loop with push_backO(n)O(n)Clear step-by-step insertion
💡
Use the vector constructor with pointers for a quick and efficient array to vector conversion.
⚠️
Forgetting to pass the pointer to one past the last element (arr + size) causes incomplete or wrong vector content.