C++ std::map Functions

About This Index
This page is the C++ std::map Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what a std::map is on /cpp/map. Use this page when you need a specific member function such as insert(), find(), or lower_bound().
A std::map<Key, T> stores key and value pairs, keeps the keys in order, and allows each key only once. Reading with m[key] adds a missing key, so prefer at() or find() to read. A map has no capacity(), reserve(), or push_back(). The 32 functions are listed below.
How to Call a Map Function
Include <map>, create the map, and call members with a dot:
#include <iostream>
#include <map>
#include <string>
std::map<std::string, int> m = {{"b", 2}, {"a", 1}};
m.insert({"c", 3}); // a=1 b=2 c=3
m.insert_or_assign("a", 10); // a=10 (C++17)
int v = m.at("b"); // 2 (throws if missing)
auto it = m.find("c"); // iterator, or m.end()
bool has = m.contains("z"); // false (C++20)
m.erase("b"); // a=10 c=3
std::size_t n = m.size(); // 2 C++ std::map Functions Table
All function guides in this C++ tutorial series (32 tutorials), grouped by purpose.
Element access
1 functionRead a value by its key. at() is the safe way. The [] operator also exists, but it adds a missing key.
Capacity
3 functionsAsk how many key and value pairs a map holds. A map has no capacity() or reserve().
Modifiers
11 functionsAdd, update, move, or remove pairs. Keys stay unique and the map stays sorted after every change.
| Function | Description | Tutorial |
|---|---|---|
clear() | Remove every pair from the map. | Try it |
insert() | Add a new key and value pair. An existing key is left alone. | Try it |
insert_or_assign() | Insert a new key, or update the value if the key exists (C++17). | Try it |
insert_range() | Insert every pair from a range in one call (C++23). | Try it |
emplace() | Build a new pair in place from its constructor arguments (C++11). | Try it |
emplace_hint() | Build a pair in place, using a hint for where it should go (C++11). | Try it |
try_emplace() | Add a pair only if the key is new, without touching your arguments (C++17). | Try it |
erase() | Remove a pair by key, by iterator, or a whole range. | Try it |
extract() | Take a pair out of the map as a node handle (C++17). | Try it |
merge() | Move pairs with new keys from another map into this one (C++17). | Try it |
swap() | Exchange the whole content of two maps quickly. | Try it |
Lookup
6 functionsSearch by key in logarithmic time. Because the keys are sorted, you can also search for the first key at or after a value.
| Function | Description | Tutorial |
|---|---|---|
count() | Tell whether a key exists: the answer is 0 or 1 for a map. | Try it |
find() | Search for a key and get an iterator to its pair, or end(). | Try it |
contains() | Return true if the key is in the map (C++20). | Try it |
equal_range() | Get the range of pairs whose key equals the given key. | Try it |
lower_bound() | Find the first pair whose key is not less than the given key. | Try it |
upper_bound() | Find the first pair whose key is greater than the given key. | Try it |
Observers
3 functionsGet the helper objects that a map uses to compare keys and to allocate memory.
| Function | Description | Tutorial |
|---|---|---|
key_comp() | Get the function object that compares two keys. | Try it |
value_comp() | Get the function object that compares two whole pairs by key. | Try it |
get_allocator() | Get a copy of the allocator the map uses for its nodes. | Try it |
Iterators
8 functionsWalk through the pairs in key order, forwards or backwards. The c versions are read-only.
| Function | Description | Tutorial |
|---|---|---|
begin() | Get an iterator to the pair with the smallest key. | Try it |
end() | Get the iterator just past the last pair. | Try it |
cbegin() | Get a read-only iterator to the first pair (C++11). | Try it |
cend() | Get a read-only iterator past the last pair (C++11). | Try it |
rbegin() | Get a reverse iterator to the pair with the biggest key. | Try it |
rend() | Get the reverse iterator just before the first pair. | Try it |
crbegin() | Get a read-only reverse iterator to the biggest key (C++11). | Try it |
crend() | Get a read-only reverse iterator before the first pair (C++11). | Try it |
Frequently Asked Questions
Start with insert()
Add a key and value pair to a map, the first thing most programs do.
12 people found this page helpful
