Create C++ STL unordered map
https:w//ww.theitroad.com
To create an std::unordered_map
container in C++, you can use the following syntax:
#include <unordered_map> std::unordered_map<key_type, value_type> myMap;
Here, key_type
and value_type
are the data types of the keys and values stored in the map, respectively. For example, if you want to create an std::unordered_map
that stores integers as keys and strings as values, you can use the following code:
#include <iostream> #include <unordered_map> #include <string> int main() { std::unordered_map<int, std::string> myMap = { {1, "one"}, {2, "two"}, {3, "three"} }; return 0; }
This program creates an std::unordered_map
container called myMap
that stores integers as keys and strings as values. It initializes the map with three key-value pairs: {1, "one"}
, {2, "two"}
, and {3, "three"}
.
Note that you can also use the insert()
method to add key-value pairs to the map one at a time, like this:
myMap.insert(std::make_pair(4, "four"));
This will add a new key-value pair to the map with a key of 4 and a value of "four".