C++ map and multimap
In this page:
#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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Using
m[key]to check for a key, which inserts a default value if it is missing; usefindorcount. - Expecting
mapto keep insertion order, when it is sorted by key. - Calling
[]on a const map, which does not compile.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: