STL Containers

STL Containers

When to use vectors, arrays, maps, sets, queues, and container adapters.

STL Containers

Sequence containers

Associative containers

Tree-based and ordered by key.

Unordered containers

Hash-based with average constant-time lookup.

Container adapters

Common operations

std::vector<int> values{1, 2, 3};
values.push_back(4);
values.emplace_back(5);
values.erase(values.begin());

Iteration

for (auto it = values.begin(); it != values.end(); ++it) {
    std::cout << *it << '\n';
}

Selection rules

Iterator invalidation quick notes

Modern container-adjacent tools

void render(std::span<const int> pixels);
std::mdspan<float, std::extents<std::size_t, 4, 4>> matrix(view_ptr);

Practical selection hints