Search code examples
c++c++11stlunordered-map

How to print the content of a nested std::unordered_map?


I'm trying to print all the content of an std::unordered_map specified like this:

std::unordered_map<uint64_t, std::unordered_map<uint64_t,uint64_t>> m;

After adding things in the map, I tried the following:

for (auto it=map.begin(); it!=map.end(); it++) {
    cout << it->first << it->second << endl;
}

but it is not working.


Solution

  • Since you have nested std::unordered_map, following should work:

    for (auto const& i : m) {
        for (auto const& j : i.second) {
            std::cout << j.first << " " << j.second << std::endl;
        }
    }