To initialize a std::map
using uniform initialization, you can follow this syntax:
std::map<KeyType, ValueType> myMap = {
{key1, value1},
{key2, value2},
{key3, value3},
// ...
};
Here, KeyType
refers to the type of the keys in the map, and ValueType
is the type of the corresponding values. You can provide as many key-value pairs as needed, separated by commas.
Let’s look at an example to make it clearer. Suppose we want to create a std::map
that stores the capital cities of different countries:
#include <map>
#include <string>
int main() {
std::map<std::string, std::string> capitalCities = {
{"USA", "Washington D.C."},
{"France", "Paris"},
{"Japan", "Tokyo"},
// ...
};
// Accessing elements
std::cout << "Capital of Japan: " << capitalCities["Japan"] << std::endl;
return 0;
}
In this example, we initialize a std::map
called capitalCities
using uniform initialization. The keys are of type std::string
, representing the country names, and the values are also of type std::string
, representing the respective capital cities.
Uniform initialization has simplified the process of initializing std::map
objects, making the code easier to read and write. It provides a cleaner and more concise way to initialize maps with key-value pairs, making the code more maintainable and less error-prone.
For more details on std::map
and uniform initialization in C++, you can refer to the C++ documentation and C++11 standard respectively.