vector
A dynamic array that grows automatically — the default container in C++.
Easy
#include <vector>
std::vector<int> v = {3, 1, 2};
v.push_back(4);
v.size(); // 4
v[0]; // 3 (no bounds check) — v.at(0) checks
for (int x : v) std::cout << x << ' ';
v.reserve(1000); // pre-allocate to avoid repeated reallocations
Elements are stored contiguously, so access is fast and cache-friendly. Adding to the end is fast on average; inserting in the middle is slow.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.