Vector vs std::array vs C Array
Compare the three sequential containers and pick one based on size, ownership, and performance.
Vector vs std::array vs C Array is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Three Sequential Containers
C++ has three ways to hold a contiguous sequence:
- std::vector — dynamic size, heap storage
- std::array — fixed size at compile time, stack storage
- C-style array — fixed size, low level
C-Style Arrays
The oldest form. Size is part of the type. Decays to a pointer when passed to functions — losing the size.
int a[5] = {1, 2, 3, 4, 5};
std::cout << a[2]; // 3Problems with C-Style Arrays
They forget their size in most contexts, do not bounds-check, and are easy to misuse. Prefer std::array or std::vector in new code.
std::array (C++11)
A thin wrapper around a fixed-size array. Same performance as a C array but with a real type and proper member functions.
#include <array>
std::array<int, 5> a = {1, 2, 3, 4, 5};
std::cout << a.size(); // 5
std::cout << a.at(2); // bounds-checkedstd::array Stays on the Stack
Unlike vector, std::array stores its elements directly. No heap allocation. Size is a template parameter.
std::vector for Dynamic Size
Use std::vector when you do not know the size at compile time or when the size will change at runtime.
Performance Comparison
For fixed sizes, all three perform similarly. std::vector adds a small overhead for the pointer-and-size bookkeeping plus one indirection.
Passing to Functions
Pass vector or std::array by const reference to avoid copies. C arrays should be replaced by std::span (C++20) when borrowing.
void process(const std::vector<int>& v);
void process(const std::array<int, 5>& a);
#include <span>
void process(std::span<const int> data); // accepts bothstd::span (C++20)
A non-owning view over a contiguous sequence. Like a pointer + length pair. Accepts vectors, std::arrays, and C arrays uniformly.
When to Choose What
Decision tree:
- Dynamic size needed →
std::vector - Fixed size, no allocation →
std::array - Borrowing without ownership →
std::span - Interop with C code → C array
Conversion Between Them
You can copy elements between types using ranges or algorithms.
std::array<int, 3> a = {1, 2, 3};
std::vector<int> v(a.begin(), a.end());
int c[3];
std::copy(a.begin(), a.end(), c);Quick Check
Which container stores elements on the stack and has a size known at compile time?
Recap
Prefer std::vector for dynamic size and std::array for fixed size. C-style arrays are rarely the best choice in modern code. Use std::span to borrow a sequence without owning it.
Frequently asked questions
Is the “Vector vs std::array vs C Array” lesson free?
Yes — the full text of “Vector vs std::array vs C Array” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Vector vs std::array vs C Array”?
Compare the three sequential containers and pick one based on size, ownership, and performance. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Vector vs std::array vs C Array” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- std::vector Basics: push_back size capacity
- Iterating Vectors: index range-for iterators
- Modifying Vectors: insert erase clear
- Vector vs std::array vs C Array