C++ Array vs Vector: When to Use Each (with Examples)
C++ array vs vector in one line: use std::vector by default for its safety and automatic resizing, and reach for a raw array only for small, fixed-size, performance-critical data.
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.
Curious what's happening underneath? Arrays, strings and vectors make much more sense once you understand pointers and memory. C++ Better Explained explains both with diagrams and plain-English analogies.
Get the book — $27Common Operations Compared
| Operation | Array | Vector |
|---|---|---|
| Declare with size | int arr[5] | vector<int> v(5) |
| Initialize with values | int arr[] = {1,2,3} | vector<int> v = {1,2,3} |
| Access element | arr[i] | v[i] or v.at(i) |
| Get size | sizeof(arr)/sizeof(arr[0]) | v.size() |
| Add element | Not possible (fixed size) | v.push_back(x) |
| Remove last | Not applicable | v.pop_back() |
| Iterate | for (int i=0; i<5; i++) | Range-for or iterator |
| Pass to function | Decays to pointer | Passes by reference cleanly |
| Sort | sort(arr, arr+5) | sort(v.begin(), v.end()) |
When to Use Each
Use std::vector when:
- You don’t know the size at compile time
- The size might change
- You’re writing a function that accepts a sequence
- You want the convenience of
.size(),.push_back(),.erase() - You want STL algorithm compatibility
Use arrays when:
- Size is fixed and known at compile time
- You need stack allocation for maximum performance in tight loops
- You’re interfacing with a C API that requires a raw array
- You’re writing embedded or systems code where heap allocation is avoided
Use std::array (the best of both worlds) when:
- Size is fixed and known at compile time
- You want value semantics and STL compatibility with no heap overhead
#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.
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.
Related Articles
- C++ Arrays Tutorial — how arrays work from scratch
- C++ Vector Tutorial — the complete guide to std::vector
- C++ STL Containers Explained — choosing between vector, list, set, and more
- Memory Management in C++ — stack vs heap
Take Your C++ Further

Arrays, strings and vectors are easier to use well once you know what's underneath them: pointers and memory. C++ Better Explained explains both, and OOP, in 87 plain-English pages with diagrams and analogies.
Get the book — $2787 pages · plain English · 30-day money-back guarantee