C++ std::map Functions

Beginner
8 min read
Updated: Nov 2026
32 function guides
Function index

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 sorted container of unique keys.

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:

C++
#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 function

Read a value by its key. at() is the safe way. The [] operator also exists, but it adds a missing key.

FunctionDescriptionTutorial
at()Read or change the value of a key. Throws if the key is missing.Try it

Capacity

3 functions

Ask how many key and value pairs a map holds. A map has no capacity() or reserve().

FunctionDescriptionTutorial
empty()Check whether the map has no elements.Try it
size()Count the key and value pairs in the map.Try it
max_size()Get the largest number of pairs the map could ever hold.Try it

Modifiers

11 functions

Add, update, move, or remove pairs. Keys stay unique and the map stays sorted after every change.

FunctionDescriptionTutorial
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 functions

Search by key in logarithmic time. Because the keys are sorted, you can also search for the first key at or after a value.

FunctionDescriptionTutorial
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 functions

Get the helper objects that a map uses to compare keys and to allocate memory.

FunctionDescriptionTutorial
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 functions

Walk through the pairs in key order, forwards or backwards. The c versions are read-only.

FunctionDescriptionTutorial
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

They are the functions you call on a std::map object with a dot, such as m.insert({"a", 1}), m.find("a"), m.erase("a"), or m.size(). They need #include <map>.
This tutorial series covers 32: element access (at), capacity (empty, size, max_size), modifiers (clear, insert, insert_or_assign, insert_range, emplace, emplace_hint, try_emplace, erase, extract, merge, swap), lookup (count, find, contains, equal_range, lower_bound, upper_bound), observers (key_comp, value_comp, get_allocator), and iterators (begin, end, cbegin, cend, rbegin, rend, crbegin, crend).
The [] operator is covered inside the at() and insert() tutorials. Remember that m[key] adds the key with a default value when it is missing, while at() throws and find() returns end().
emplace(), emplace_hint(), at(), and the c-iterators are C++11. insert_or_assign(), try_emplace(), extract(), and merge() are C++17. contains() is C++20. insert_range() is C++23.
Read the C++ std::map page first. Then open insert(), find(), at(), and erase() from the tables below.

Start with insert()

Add a key and value pair to a map, the first thing most programs do.

insert() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful