std::map#include <map> stores key-value pairs sorted by key, where each key is unique. The elements are stored in a balanced binary search tree (usually a red-black tree), so inserting, finding and removing a key have O(log n) time complexity. The key and value types are given as template arguments. It is available from the map header:

#include <map>

std::map#include <map><std::string#include <string>, int> ages;

[] accesses the value for a key. If the key doesn't exist, it is inserted with a value-initialised value:

ages["Alice"] = 30;
ages["Bob"] = 25;
ages["Carol"];  // Inserts "Carol" with 0.
ages.size();    // 3

find() returns an iterator to a pair of key and value, or end() if the key doesn't exist:

std::map#include <map><std::string#include <string>, int>::iterator it = ages.find("Alice");
if (it != ages.end()) {
  it->first;   // "Alice"
  it->second;  // 30
}
auto it = ages.find("Alice");
if (it != ages.end()) {
  it->first;   // "Alice"
  it->second;  // 30
}

The key can't be modified through an iterator, since changing the key can break the sorted order:

it->first = "Alicia";  // ERROR.
it->second = 31;       // OK.

count() returns 1 if the key exists and 0 otherwise:

ages.count("Alice");  // 1
ages.count("Dave");   // 0

contains() is a clearer way to check whether a key exists:

ages.contains("Alice");  // true
ages.contains("Dave");   // false

insert() adds a pair but does not overwrite an existing key:

ages.insert(std::make_pair#include <utility>("Dave", 40));
ages.insert(std::make_pair#include <utility>("Alice", 99));  // Ignored.
ages["Alice"];                             // 30
ages.insert({"Dave", 40});
ages.insert({"Alice", 99});  // Ignored.
ages["Alice"];               // 30

Keys are removed with erase():

ages.erase("Bob");
ages.count("Bob");  // 0

A map can be initialised with a list of key-value pairs:

std::map#include <map><std::string#include <string>, int> ages = {{"Alice", 30}, {"Bob", 25}};

at() accesses the value for a key but throws an exception instead of inserting when the key doesn't exist:

ages.at("Alice");  // 30
ages.at("Dave");   // Throws std::out_of_range#include <stdexcept>.

Maps work with range-based for loops and are always iterated in key order:

for (const auto& p : ages) {
  std::cout#include <iostream> << p.first << ": " << p.second << "\n";
}
for (const auto& [name, age] : ages) {
  std::cout#include <iostream> << name << ": " << age << "\n";
}

Multimap

std::multimap is a sorted map that allows duplicate keys. It is also available from the map header. It has no [], since a key can have several values, so key-value pairs are added with insert():

std::multimap<std::string#include <string>, int> scores;
scores.insert(std::make_pair#include <utility>("Alice", 90));
scores.insert(std::make_pair#include <utility>("Alice", 85));
scores.insert(std::make_pair#include <utility>("Bob", 70));
scores.count("Alice");  // 2
std::multimap<std::string#include <string>, int> scores = {
  {"Alice", 90},
  {"Alice", 85},
  {"Bob", 70}
};
scores.count("Alice");  // 2

equal_range() returns a pair of iterators covering every value for a key:

typedef std::multimap<std::string#include <string>, int>::iterator Iter;
std::pair#include <utility><Iter, Iter> range = scores.equal_range("Alice");
for (Iter it = range.first; it != range.second; ++it) {
  std::cout#include <iostream> << it->second << "\n";  // 90, then 85
}
auto [first, last] = scores.equal_range("Alice");
for (auto it = first; it != last; ++it) {
  std::cout#include <iostream> << it->second << "\n";  // 90, then 85
}
auto [first, last] = scores.equal_range("Alice");
for (const auto& [name, score] : std::ranges::subrange#include <ranges>(first, last)) {
  std::cout#include <iostream> << score << "\n";  // 90, then 85
}

Unordered Map

std::unordered_map stores key-value pairs with unique keys in a hash table. Finding, inserting and removing take O(1) time on average, but the key-value pairs are in no particular order. It is available from the unordered_map header:

#include <unordered_map>

std::unordered_map<std::string#include <string>, int> ages = {{"Alice", 30}, {"Bob", 25}};
ages["Carol"] = 35;
ages.count("Alice");  // 1

It has the same [], at(), insert(), find(), count() and erase() methods as std::map#include <map>.

Note

Use an unordered map when lookups need to be fast and order does not matter. The key type needs a hash function, which built-in types and std::string#include <string> already have through std::hash.

Unordered Multimap

std::unordered_multimap is an unordered map that allows duplicate keys. It is also available from the unordered_map header:

std::unordered_multimap<std::string#include <string>, int> scores = {{"Alice", 90}, {"Alice", 85}};
scores.count("Alice");  // 2

Flat Map

std::flat_map stores key-value pairs with unique keys in two sequence containers (std::vector#include <vector> by default), one for keys kept in sorted order and one for values. It is available from the flat_map header:

#include <flat_map>

std::flat_map<std::string#include <string>, int> ages = {{"Bob", 25}, {"Alice", 30}};
ages["Carol"] = 35;
ages.contains("Alice");  // true

Finding a key uses binary search, which takes O(log n) time. Iterating is faster than std::map#include <map> because the keys and values are contiguous in memory, but inserting and erasing take O(n) time since later elements have to shift.

Note

Use a flat map when keys are rarely inserted or erased but often looked up, when keys need to be sorted, or use less memory than std::map#include <map>.

Flat Multimap

std::flat_multimap is a flat map that allows duplicate keys. It is also available from the flat_map header:

std::flat_multimap<std::string#include <string>, int> scores = {{"Alice", 90}, {"Alice", 85}};
scores.count("Alice");  // 2

References