C++ Initialize an Unordered Map

You can initialize an std::unordered_map container in C++ using the curly brace syntax {}. Here's an example:

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_map<std::string, int> myMap = {
        {"apple", 5},
        {"banana", 2},
        {"orange", 3}
    };

    return 0;
}
Source:w‮itfigi.ww‬dea.com

In this program, an std::unordered_map called myMap is initialized with three key-value pairs: "apple" maps to 5, "banana" maps to 2, and "orange" maps to 3. Note that the data types of the keys and values are specified in the angle brackets (< and >).

You can also initialize an empty std::unordered_map like this:

std::unordered_map<std::string, int> myMap;

This creates an empty map that you can populate later using the insert() method, as shown in the previous answer.