example2_basic_use.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_multimap<std::string, std::string> country_capitals;

    // Inserting multiple entries for the same key
    country_capitals.insert({"USA", "Washington D.C."});
    country_capitals.insert({"USA", "New York City"});
    country_capitals.insert({"France", "Paris"});
    country_capitals.insert({"Japan", "Tokyo"});

    // Counting entries for a key
    std::cout << "USA has " << country_capitals.count("USA") << " entries" << std::endl;

    // Finding all entries for a key
    auto range = country_capitals.equal_range("USA");
    for (auto it = range.first; it != range.second; ++it) {
        std::cout << "USA: " << it->second << std::endl;
    }

    return 0;
}
Back to unordered_map