I'm stuck accessing a 2D map
How do I retrieve data out of the inner map?

map <string, map <int, class> > myMap

When I set up and iterator it, I get a std::pair.
it->second should yield a pair aswell because it points to map <int, class>
but I can't make a std::pair out of it->second.

it->second isn't recognized as a pair.

8 Years
Discussion Span
Last Post by nucleon

I'm not sure what the keyword "class" is doing in your map declaration, but using "int" would work like this:

#include <iostream>
#include <string>
#include <map>
using namespace std;

typedef map<int, int>              t_map_inner;
typedef t_map_inner::iterator      it_inner;
typedef map<string, t_map_inner >  t_map_outer;
typedef t_map_outer::iterator      it_outer;

int main() {
    t_map_outer m;
    m["abc"][3] = 5;
    m["abc"][7] = 2;
    m["def"][4] = 7;

    for (it_outer i = m.begin(); i != m.end(); ++i) {
        cout << i->first << '\n';
        for (it_inner j = i->second.begin(); j != i->second.end(); ++j)
            cout << "  " << j->first << ", " << j->second << '\n';
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.