In C++, std::map<Key, T> is a container that stores key and value pairs. You look up a value by its key, like looking up a word in a dictionary. The keys are unique and the map always keeps them in sorted order.
Inside, a map is a balanced tree, so finding, inserting, and erasing a key all take logarithmic time. Read a value with at() or find(), and add pairs with insert() or emplace(). Include <map> to use it.
This page teaches what a std::map is. The Map Functions index lists every function guide in this series.
Why choose a map?
Choose a map when you need to connect keys to values and want the keys in order, for example names to ages, words to counts, or grade limits to letters. If you do not care about the order and want the fastest lookup, a std::unordered_map is often a better fit. If you only need a list of values by position, use a std::vector.
Syntax
C++ Syntax
Common ways to create and use a std::map:
C++
#include <iostream>
#include <map>
#include <string>
std::map<std::string, int> a; // empty map
std::map<std::string, int> b = {{"b", 2}, {"a", 1}}; // sorted: a, b
std::map<int, std::string, std::greater<int>> c; // biggest key first
b["c"] = 3; // add or update with []
b.insert({"d", 4}); // add only if the key is new
b.emplace("e", 5); // build the pair in place
int x = b.at("a"); // 1 (throws if missing)
auto it = b.find("c"); // iterator, or b.end()
b.erase("d"); // remove by key
std::size_t n = b.size(); // number of pairs
Reference
std::map vs std::unordered_map vs std::vector
Here is a quick comparison of three containers that beginners often mix up:
Feature
std::map
std::unordered_map
std::vector
Stores
Key and value pairs
Key and value pairs
Values only
Order of elements
Sorted by key
No order
The order you added them
Find by key
Logarithmic
Usually constant
Linear (search by value)
Duplicate keys
Not allowed
Not allowed
Not applicable
Ordered search (lower_bound())
Yes
No
Only if you sort it
Key type needs
operator< or a comparator
A hash and operator==
Not applicable
Header
<map>
<unordered_map>
<vector>
Guide
When to Use find(), at() or []
All three read a value by key, but they react differently when the key is missing. Pick the one that fits:
std::map<Key, T> is a container from <map>. It stores key and value pairs, keeps the keys sorted, and allows each key only once. You use a key to find its value quickly.
A map keeps keys in sorted order and finds a key in logarithmic time. An unordered_map uses hashing, has no order, and is usually faster to find a key. Choose map when you need sorted output or range searches such as lower_bound().
find() returns an iterator and gives end() for a missing key. at() returns the value but throws std::out_of_range for a missing key. m[key] never fails, but it adds a missing key with a default value, which changes the map.
No. Each key appears once. insert() and emplace() skip an existing key, while m[key] = value and insert_or_assign() replace its value. If you need duplicates, use std::multimap.
Open the C++ std::map Functions index. It lists every member function, from insert() and find() to lower_bound() and extract(), each with its own tutorial.