Skip to content
C++ Better Explained
Go back
C++ Array vs Vector: Which One Should You Use?
Edit page

C++ Array vs Vector: Which One Should You Use?

C++ gives you two main ways to store sequences of values: old-style arrays inherited from C, and std::vector from the standard library. Beginners often wonder which to use.

The short answer: use std::vector by default. Here’s why — and when arrays are the right choice.


The Key Differences

Fixed Size vs Dynamic Size

Arrays have a fixed size set at compile time:

int arr[5];       // Always exactly 5 integers
arr[5] = 10;      // Out of bounds — undefined behavior, no error

Vectors grow automatically:

vector<int> v;    // Starts empty
v.push_back(1);   // Now has 1 element
v.push_back(2);   // Now has 2 elements
v.push_back(3);   // Now has 3 elements
// v grows as needed — no size limit

Size Tracking

Arrays don’t know their own size:

int arr[5] = {1, 2, 3, 4, 5};
// No arr.size() — you must track the size yourself
int size = 5;  // Manual tracking is error-prone

Vectors always know their size:

vector<int> v = {1, 2, 3, 4, 5};
cout << v.size();  // 5 — always accurate

Function Parameter Decay

Arrays “decay” to pointers when passed to functions, losing their size:

void print(int arr[], int size) {  // Must pass size separately
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
}

int arr[5] = {1, 2, 3, 4, 5};
print(arr, 5);  // Must manually pass the size

Vectors pass cleanly and keep their size:

void print(const vector<int>& v) {  // v.size() works inside
    for (int x : v) {
        cout << x << " ";
    }
}

vector<int> v = {1, 2, 3, 4, 5};
print(v);  // Size travels with the vector

Bounds Checking

Arrays do not check bounds at all:

int arr[3] = {1, 2, 3};
cout << arr[10];  // No error — reads garbage memory (or crashes)

Vectors offer .at() with bounds checking:

vector<int> v = {1, 2, 3};
cout << v[10];    // No error — reads garbage (same as array)
cout << v.at(10); // Throws std::out_of_range exception

Memory: Stack vs Heap

Arrays (local) live on the stack:

int arr[1000];  // 4,000 bytes on the stack (may overflow for very large arrays)

Vectors store their data on the heap:

vector<int> v(1000);  // 4,000 bytes on the heap (no stack limit issue)

Stack space is limited (typically 1–8 MB). For large collections, vectors are safer.


Common Operations Compared

OperationArrayVector
Declare with sizeint arr[5]vector<int> v(5)
Initialize with valuesint arr[] = {1,2,3}vector<int> v = {1,2,3}
Access elementarr[i]v[i] or v.at(i)
Get sizesizeof(arr)/sizeof(arr[0])v.size()
Add elementNot possible (fixed size)v.push_back(x)
Remove lastNot applicablev.pop_back()
Iteratefor (int i=0; i<5; i++)Range-for or iterator
Pass to functionDecays to pointerPasses by reference cleanly
Sortsort(arr, arr+5)sort(v.begin(), v.end())

When to Use Each

Use std::vector when:

Use arrays when:

Use std::array (the best of both worlds) when:

#include <array>
array<int, 5> a = {1, 2, 3, 4, 5};  // Fixed size, but has .size(), works with STL
cout << a.size();  // 5
sort(a.begin(), a.end());

std::array is a fixed-size array wrapped in a class — it has all the STL benefits without dynamic allocation.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Practical Example: Comparing the Two

With array:

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int scores[5] = {85, 92, 78, 95, 88};
    int size = 5;

    sort(scores, scores + size);

    for (int i = 0; i < size; i++) {
        cout << scores[i] << " ";
    }
    return 0;
}

With vector:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> scores = {85, 92, 78, 95, 88};

    sort(scores.begin(), scores.end());

    for (int score : scores) {
        cout << score << " ";
    }
    return 0;
}

The vector version is cleaner — no manual size tracking, range-based for loop works, and you could add scores.push_back(100) at any point.


The Recommendation

For beginners: start with std::vector. It’s the right tool for almost every situation you’ll encounter. Switch to raw arrays only when you have a specific reason — and consider std::array as a middle ground when you need fixed size with STL compatibility.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ Calculator Program: Build One Step by Step
Next Post
C++ const vs constexpr: What's the Difference?