Arrays and vectors are the workhorses of DSA: a fixed block you index instantly, and its growable cousin.
1 Contiguous memory
Elements sit side by side, so a[i] is one address calculation: start + i * sizeof(int). That is why indexing is O(1).
2 Array vs vector
int a[5] is fixed at compile time. vector<int> v grows at runtime by doubling its capacity — amortized O(1) per push_back.
3 Middle ops cost O(n)
Insert or erase anywhere except the end and every later element slides one slot — the animation below shows the shift.
4 Iterator invalidation
When push_back reallocates, the block moves — old iterators and pointers point at freed memory.
5 Range-for
for (int x : v) visits every element without index math — the safest way to read a vector.
The big picture
Contiguous memory1Elements sit side by side, so a[i] is one address calculation: start + i * sizeof(int). That is why indexing is O(1). Array vs vector2int a[5] is fixed at compile time. vector<int> v grows at runtime by doubling its capacity — amortized O(1) per push_back. Middle ops cost O(n)3Insert or erase anywhere except the end and every later element slides one slot — the animation below shows the shift. Iterator invalidation4When push_back reallocates, the block moves — old iterators and pointers point at freed memory. Range-for5for (int x : v) visits every element without index math — the safest way to read a vector.
5 building blocks make up The big picture
v.erase(v.begin() + 2) — four copies, one pop_back, order preserved.
1 / 12A full vector of seven IDs. We erase index 2 (the value 33) — everything after it must slide left.
How a vector grows: construct, push, double, index, erase — click each stage for the mechanics.
The vector API in motion, then an in-place reverse with two pointers.
mainmain begins — the vector story starts empty.
Memory
no variables yet — press play
Console
output appears as the program runs…
An int array in 3D: four boxes side by side, each written by a bare address calculation.
Drag to orbit the scene. Press Play to watch memory in action.
push_back, erase and a fresh element — track size and positions carefully.
Your predicted output — one line per console line
Hint: erase(v.begin() + 1) removes the value at index 1 and shifts the rest left. size() counts live elements.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?v[i]drops into place
?push_backdrops into place
?pop_backdrops into place
?erase(mid)drops into place
?int a[5]drops into place
?range-fordrops into place
?v.at(9)drops into place
?iterator invalidationdrops into place
01Arrays are fixed; vectors grow by doubling capacity.
02Indexing is O(1) because elements sit side by side in memory.
03Middle insert and erase pay O(n) — every later element shifts.
04push_back may reallocate and copy — iterators die in the move.
05pop_back is always O(1); erase(begin()) costs O(n).
06Prefer v.at(i) while learning — it catches out-of-range bugs.