Header Reference: <vector>
Header Reference: <vector>
The main types, operations, guarantees, and usage patterns associated with std::vector.
Header Reference: <vector>
The main types, operations, guarantees, and usage patterns associated with std::vector.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
Header reference pages are meant to answer a practical question quickly: what this header provides, when to reach for it, and which usage rules are easiest to get wrong.
<vector>std::vector<T, Allocator>operator[], at, front, back, databegin, end, reverse iterators, and contiguous storage compatibilitysize, empty, capacity, reserve, shrink_to_fitpush_back, emplace_back, insert, emplace, erase, clear, resize, assign, swapstd::span, and low-level contiguous APIsat() checks bounds; operator[] does notreserve() can avoid repeated reallocations when growth is predictablepush_back and emplace_back are amortized constant time, but can trigger full reallocationclear() destroys elements but usually does not release capacityreserve() changes capacity without changing size#include <vector>
#include <span>
void normalize(std::span<int> values) {
for (int& value : values) {
value *= 2;
}
}
int main() {
std::vector<int> values{1, 2, 3};
values.reserve(6);
values.emplace_back(4);
normalize(values);
}
vector as the default owning sequence container unless another structure is justifiedemplace_back or push_back based on clarity, not habit alone#include <vector>
#include <iostream>
int main() {
std::vector<int> values{1, 2, 3};
values.reserve(8);
values.push_back(4);
for (int value : values) {
std::cout << value << ' ';
}
}