0
0
CppComparisonBeginner · 4 min read

Vector vs Array in C++: Key Differences and Usage Guide

In C++, a vector is a dynamic array that can resize automatically, while a built-in array has a fixed size known at compile time. vector offers more flexibility and safety features, but array can be faster and uses less memory when size is fixed.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of vector and built-in array in C++.

Featurestd::vectorBuilt-in array
SizeDynamic, can grow or shrink at runtimeFixed, size set at compile time
MemoryUses heap memory, may allocate more than neededUsually uses stack memory, fixed size
PerformanceSlight overhead due to dynamic resizingFaster access, no resizing overhead
SafetyBounds checking with at() methodNo built-in bounds checking
InitializationCan be default or value initialized easilyMust be manually initialized or default
Use caseWhen size changes or unknown at compile timeWhen size is fixed and known upfront
⚖️

Key Differences

The main difference between vector and built-in array is flexibility. A vector can change its size during program execution, allowing you to add or remove elements easily. This is because vector manages memory dynamically on the heap, growing its storage as needed.

In contrast, a built-in array has a fixed size that must be known when you write the code. Its size cannot change at runtime, and it usually lives on the stack, which makes it faster but less flexible. Also, built-in arrays do not provide safety features like bounds checking, so accessing out-of-range elements can cause bugs or crashes.

Additionally, vector provides useful member functions like push_back(), size(), and at() for safer and easier manipulation. Built-in arrays are simpler but require manual handling for size and safety.

⚖️

Code Comparison

Here is how you create and use a fixed-size built-in array in C++:

cpp
#include <iostream>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}
Output
10 20 30 40 50
↔️

Vector Equivalent

Here is the equivalent code using std::vector which can resize dynamically:

cpp
#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {10, 20, 30, 40, 50};
    vec.push_back(60); // Add an element dynamically
    for (size_t i = 0; i < vec.size(); ++i) {
        std::cout << vec[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}
Output
10 20 30 40 50 60
🎯

When to Use Which

Choose std::vector when you need a container that can grow or shrink during runtime, or when you want safer access with bounds checking. It is ideal for most cases where the number of elements is not fixed or known in advance.

Choose a built-in array when you know the size at compile time and want the fastest possible access with minimal memory overhead. It is suitable for small fixed-size collections where performance is critical and flexibility is not needed.

Key Takeaways

Use vector for dynamic, resizable collections with safety features.
Use built-in array for fixed-size, fast, low-overhead storage.
vector manages memory on the heap; arrays usually live on the stack.
Built-in arrays lack bounds checking; vector offers safer access methods.
Pick based on whether your data size changes or stays constant during execution.