On this page
std::unordered_map
Prev: std::string_view | Up: containers | Next: std::optional
Rel: std::map, Structured Bindings
Ref: cppreference — std::unordered_map
Tags: #public #prog #cpp #stdlib #containers #tier/core
Overview
std::unordered_map<K, V> (<unordered_map>) is a hash table offering average O(1) lookup, insertion, and deletion with arbitrary iteration order.
Idiomatic Usage
#include <string>
#include <unordered_map>
std::unordered_map<std::string, int> counts;
counts.try_emplace("cpp", 1); // inserts only if key is absent
if (auto it = counts.find("cpp"); it != counts.end()) {
it->second += 1;
}
if (counts.contains("cpp")) { // C++20
// key exists
}
Gotcha
map[key]inserts a default-constructed value ifkeyis missing—use.find(key)or.contains(key)for read-only lookups.
Linked from
Related notes
- tuts/cpp/stdlib/containers/map Linked here; links to this page; shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/optional Linked here; links to this page; shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/string_view Linked here; links to this page; shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers Linked here; links to this page; shared tags: #containers, #cpp, #prog, #stdlib
- tuts/cpp/builtins/structured_bindings Linked here; links to this page; shared tags: #cpp, #prog, #tier
- tuts/cpp/stdlib/containers/array shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/expected shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/span shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/string shared tags: #containers, #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/containers/variant shared tags: #containers, #cpp, #prog, #stdlib, #tier