← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 9 of 15

C++ map and multimap

A map stores pairs of a key and a value, like a phone book where you find a number by name. It keeps the keys in sorted order, and multimap allows repeated keys.
Syntax
cpp
#include <map>

std::map<key_type, value_type> map_name;
map_name[key] = value;
map_name.at(key);

What is a map?

std::map is an associative container that stores key-value pairs in sorted order by key, implemented internally as a balanced binary search tree (typically red-black tree). Keys are unique and lookups, insertions, and deletions run in O(log n) time.

Example: What is a map?

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>
#include <string>

// Program execution starts in main()
int main() {
	std::map<std::string, int> ages;
	ages["Alex"] = 30;
	// Print to the console with cout
	std::cout << ages["Alex"] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing and Updating Values

You can access values with operator[] (which inserts a default value if the key is missing) or with at() (which throws out_of_range for a missing key). Assigning to an existing key updates its value.

Example: Accessing and Updating Values

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>
#include <string>

// Program execution starts in main()
int main() {
	std::map<std::string, int> ages;
	ages["Alex"] = 30;
	ages["Alex"] = 31;
	// Print to the console with cout
	std::cout << ages.at("Alex") << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

map vs multimap

std::map requires unique keys -- inserting a duplicate key overwrites the existing value via operator[]. std::multimap allows multiple entries with the same key, storing them adjacently in sorted order.

Example: map vs multimap

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>

// Program execution starts in main()
int main() {
	std::map<int, int> m;
	m[1] = 10;
	m[1] = 20;
	// Print to the console with cout
	std::cout << m[1] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Iterating over a map

Iterating a map visits key-value pairs in ascending key order. Each element is a std::pair, accessed via .first (key) and .second (value), or with structured bindings in C++17.

Example: Iterating over a map

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>
#include <string>

// Program execution starts in main()
int main() {
	std::map<std::string, int> ages = {{"Alex", 30}, {"Sam", 25}};
	for (const auto &[name, age] : ages) {
		// Print to the console with cout
		std::cout << name << ":" << age << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Searching and Erasing

find() returns an iterator to the element or end() if the key is missing. count() returns 1 (map) or the number of matches (multimap). erase() removes an entry by key or iterator.

Example: Searching and Erasing

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>
#include <string>

// Program execution starts in main()
int main() {
	std::map<std::string, int> ages = {{"Alex", 30}};
	// Declare it and set it to ages.find("Alex")
	auto it = ages.find("Alex");
	if (it != ages.end()) std::cout << "Found: " << it->second << std::endl;
	ages.erase("Alex");
	// Print to the console with cout
	std::cout << ages.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using m[key] to check for a key, which inserts a default value if it is missing; use find or count.
  2. Expecting map to keep insertion order, when it is sorted by key.
  3. Calling [] on a const map, which does not compile.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.