std::map is the C++ standard library implementation of a dictionary. It contains key-value pairs where the keys are all unique and the key values are used to find the associated values, much like a real-world dictionary. The map is, just like most of the standard library, implemented using templates and the types of the keys and the values are given when you create the map. Some examples of maps:
|
// this is a map, where the keys are integers and the values are strings std::map<int, std::string> stringLookupTable; // this is a map where the keys are strings and the values integers std::map<std::string, int> passwordHashForUser; |
Each element of the map is of the type std::map<K, V>::value_type which is just a redefinition of std::pair<const K, V>.
Getting one element
To get the value associated with a specific key, use the bracket operator ([]). This operator will create the key-value pair if it doesn’t exist already.
|
// a map where the keys are integers and the values are strings std::map<int, std::string> userNameForUserID; // insert a user userNameForUserID[0] = std::string("John Doe"); // retrieve a user std::cout << "User #0 is named: " << userNameForUserID[0] << std::endl; |
Checking if a key exists in map
There are two ways to check if a key exists in a map.
- Using the function std::map::count(). This returns the number of keys which matches the queried key, and since all keys in the std::map are unique can this only be zero or one. This is a good way to do it if you just need to see if the key exists and don’t need to do anything with the map.
- Using the function std::map::find(). This returns an iterator to the found object, or std::map::end() if the object is not found. This is a better choice if you need to update the map, since you will get an iterator to the found key value pair.
Iteration
To iterate over all elements in the dictionary use range-based for:
|
std::map<int, std::string> values; // ... for(std::map<int, std::string>::value_type& x : values) { std::cout << x.first << "," << x.second << std::endl; } |
or much simpler using auto
|
std::map<int, std::string> values; // ... for(auto& x : values) { std::cout << x.first << "," << x.second << std::endl; } |
Notice that it is a good idea to make the variable in the loop a reference since this avoids copying the element and gives better performance. Also if you don’t intend to change the element then you can make this clear by also making it const.
Erasing one element
You can erase one element from the map by using the function erase(…) which takes either an iterator, a key or a two iterators defining a range of values to remove.
|
std::map<int, std::string> userNameForUserID; // insert a couple of user userNameForUserID[2971] = std::string("John Doe"); userNameForUserID[7362] = std::string("Jane Smith"); userNameForUserID[7952] = std::string("A Nonymous"); // Remove a user by key userNameForUserID.erase(2971); // removes the entry [2971, "John Doe"] // Remove a user by iterator auto iterator = userNameForUserID.find("Jane Smith"); userNameForUserID.erase(iterator); // removes the entry [7362, "Jane Smith"] |


