vector in C++
std::vector<T> is the default container: a dynamically resizing array with contiguous storage, O(1) indexed access, amortized O(1) push_back, and full value semantics (copying a vector copies its elements). For the overwhelming majority of sequential data, vector beats every node-based container on both speed and simplicity.
Growth doubles capacity as needed (so push_back stays amortized constant), reserve() pre-allocates when the final size is known, and shrink_to_fit() can release slack. Iterators and references invalidate on reallocation — index carefully across growth, or reserve first.
Contiguous and indexed
v[i] reaches any element in O(1); at(i) adds bounds checking that throws instead of corrupting.
push_back grows amortized O(1)
Appends usually just write into spare capacity; occasional doubling reallocations average out to constant time.
reserve before big fills
v.reserve(10000); allocates once up front — no repeated reallocations, no iterator invalidation mid-fill.
Value semantics
Copying, comparing, and assigning vectors works element-wise out of the box — no manual memory code, ever.
#include <iostream>#include <vector> int main() { std::vector<int> v; v.reserve(3); v.push_back(10); v.push_back(20); v.push_back(30); std::cout << v.size() << ", " << v[1] << std::endl; return 0;}Contiguous storage; reallocation on growth invalidates iterators.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.