C++ Containers and Views

C++ Containers and Views

Sequence containers, associative containers, adapters, spans, mdspan, and the main rules that matter when choosing among them.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

C++ Containers and Views

Sequence containers

Ordered associative containers

These are tree-based and maintain sorted order.

Unordered associative containers

These are hash-based and trade ordering for average constant-time lookup.

Container adapters

Adapters restrict the underlying interface to emphasize a specific data-structure behavior.

Views and non-owning wrappers

High-value choice rules

Important operational facts

Example in practice

#include <map>
#include <string>

int main() {
    std::map<std::string, int> counts{std::pair{"Ada", 1}, std::pair{"Bjarne", 2}};
    auto [it, inserted] = counts.insert({"Grace", 3});
    return inserted ? it->second : 1;
}