On this page
  1. std::unordered_map
  2. Overview
  3. Idiomatic Usage
  4. Gotcha

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

Linked from

Related notes