std::unordered_map
std::unordered_map<K, V> is a hash table: each key appears once, order is unspecified, and lookup, insert, and erase are average O(1). Worst case is O(n) if hashes collide.
Detailed Explanation
You are caching sessions by token. You do not need the tokens sorted. You need get and put to be cheap. That is unordered_map. It is the default dictionary when order does not matter and the key is hashable.
The table is an array of buckets. Each key's std::hash<K> picks a bucket. Equal keys (via std::equal_to<K>, which uses ==) must have equal hashes. If you specialise hash, specialise or write == to match.
find, at, and operator[] mean the same as in std::map, including the " [] inserts" trap. Iteration order is not key order and is not insertion order. Rehashing can scramble iteration order. Never depend on it.
Load factor is size() / bucket_count(). When it exceeds max_load_factor() (1.0 by default), the table rehashes: it allocates a larger bucket array and moves nodes. reserve(n) sets the bucket count so that n inserts will not rehash. Call it when you know the size.
Iterator invalidation differs from map. Insert invalidates iterators if it rehashes, but references to elements remain valid on most implementations because elements live in nodes (C++ requires that references and pointers to elements stay valid through rehash; iterators do not). Erase invalidates only iterators to the erased element. Still, do not hold iterators across a burst of inserts without reserve.
Custom keys need a hash. For a struct Key { std::string a; int b; }, you write a functor that mixes hash<string>{}(a) and hash<int>{}(b) (XOR alone is a weak mix). C++20 transparent hashing lets you look up with a string_view without allocating a string key; you need a hash and equal_to marked is_transparent.
Worst-case O(n) is not theoretical if an attacker controls the keys and you use a predictable hash. For untrusted keys, use a library with per-process salting, or do not expose the table to those keys.
Use map when you need order or lower_bound. Use unordered_map when you need average constant lookup. Use vector<pair<K, V>> plus a scan when n is tiny. unordered_multimap allows duplicate keys.
Code Examples
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> hits;
hits.reserve(8);
++hits["/"];
++hits["/"];
++hits["/about"];
std::cout << hits["/"] << '\n'; // prints: 2
if (hits.find("/login") == hits.end()) {
std::cout << "missing\n"; // prints: missing
}
}#include <cstddef>
#include <string>
#include <unordered_map>
struct Key {
std::string host;
int port;
bool operator==(const Key& o) const {
return host == o.host && port == o.port;
}
};
struct KeyHash {
std::size_t operator()(const Key& k) const {
return std::hash<std::string>{}(k.host)
^ (std::hash<int>{}(k.port) << 1);
}
};
int main() {
std::unordered_map<Key, int, KeyHash> sessions;
sessions[{ "localhost", 8080 }] = 1;
}Common Gotchas
`operator[]` inserts a default `V`
same bug as `map`. Use `find` or `at` to query.
Iteration order is not stable
a rehash, or a different library, reorders elements. Do not persist "the first element" as if it were the smallest key.
Hash and `==` must agree
if two keys compare equal but hash differently, the table can store duplicates and fail to find them. If they hash the same and compare unequal, they only collide.
`reserve` before a bulk insert
repeated rehashes copy the bucket array. One `reserve(n)` avoids that.