C++ Interview Question

What is the difference between a vector and an array in C++?

Updated 2026-08-16 · Beginner friendly
Quick answer

A raw array has a fixed size set at compile time and does not know its own length. A std vector is a dynamic array that can grow and shrink at run time, tracks its size, and manages its own memory. Vectors are safer and more flexible, so they are the default choice, while raw arrays are used only for simple fixed size or low level cases.

Key takeaways
  • A raw array has a fixed compile time size and does not track its own length.
  • A std vector is a dynamic array that resizes, tracks its size, and manages its memory.
  • Prefer vector by default and use raw arrays only for small fixed cases.

Quick comparison

#include <vector>
std::vector<int> v;
v.push_back(1);      // grows automatically
int arr[3] = {1,2,3}; // fixed size, cannot grow
In the interview

Say prefer std vector in modern C++ because it manages memory and size for you, and use raw arrays only when you must. That practical stance matches how experienced C++ developers actually code.

Frequently asked questions

How does a vector grow when it runs out of space?

It allocates a larger block, usually doubling capacity, copies or moves the elements over, and frees the old block, giving amortised O(1) appends.

What is the difference between at() and the index operator?

at() checks bounds and throws an exception if the index is invalid, while the index operator does not check and gives undefined behaviour out of range.

Want the full C++ guide?

Read every C++ concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the C++ guide All C++ questions