map & unordered_map in C++

From the C++ cheat sheet · STL Containers · verified Jul 2026

map & unordered_map

Key-value associative containers.

cpp
// Quick Reference
#include <map>            // ordered
#include <unordered_map>  // hashed
std::unordered_map<std::string, int> m;
m["a"] = 1;  m.at("a");
m.count("a");  m.contains("a"); // C++20
💡 operator[] inserts a default value if the key is missing - use .at() or .find() to just read.
⚡ Iterate with structured bindings: for (auto& [k, v] : map).
📌 std::map is ordered (tree, O(log n)); std::unordered_map is hashed (avg O(1)).
🟢 .contains() (C++20) is the readable way to test membership without inserting.
mapunordered-map

More C++ tasks

Back to the full C++ cheat sheet