Dynamic Arrays (std::vector)¶
In C++, std::vector is the default dynamic array: contiguous storage, \(O(1)\) random access, size that grows as you append.
Core operations¶
| Operation | Typical complexity |
|---|---|
push_back |
Amortized \(O(1)\) |
pop_back |
\(O(1)\) |
operator[] / at |
\(O(1)\) |
insert / erase in middle |
\(O(n)\) |
reserve(n) |
Preallocates capacity without changing size() |
Competitive programming habits¶
- Call
reservewhen the final size is known to avoid repeated reallocations. - Prefer
emplace_backwhen constructing in place. - Use
vector<bool>carefully (proxy reference); for bitsets usestd::bitsetor manual masks.
Related¶
- Prefix Arrays — static cumulative arrays
- Segment Tree / BIT — tree arrays often
vectorbacked