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.
C++ Containers and Views
Sequence containers, associative containers, adapters, spans, mdspan, and the main rules that matter when choosing among them.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
std::array: fixed-size contiguous sequencestd::vector: dynamic contiguous sequence and default first choice in most codestd::deque: efficient growth at both endsstd::list: doubly-linked list, niche but stable iteratorsstd::forward_list: singly-linked list for specialized casesstd::map, std::multimapstd::set, std::multisetThese are tree-based and maintain sorted order.
std::unordered_map, std::unordered_multimapstd::unordered_set, std::unordered_multisetThese are hash-based and trade ordering for average constant-time lookup.
std::stackstd::queuestd::priority_queueAdapters restrict the underlying interface to emphasize a specific data-structure behavior.
std::span: contiguous view over existing storagestd::mdspan: multidimensional non-owning viewstd::vectorstd::array when size is compile-time fixedstd::unordered_map or std::unordered_set when ordering does not matterstd::span for read-only or borrowed contiguous access parametersvectorreserve() can reduce reallocations in vector and unordered_* containers#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;
}