How can I iterate over a map with a pair as key?

c++, dictionary, iterator, output, stdmap

Solution

Well, map Key is first item, but itself it is a pair

So

pair<int,int>& key(*it);
cout << key.first << " " << key.second << "\n";

might do the trick

Problem

I have a `map` with a `pair<int, int>` as key and a third integer as value. How can I iterate over the map's keys in order to print them? My example code is pasted below: ``` #include <iostream> #include <map> #include <algorithm> using namespace std; int main () { map <pair<int, int>, int> my_map; pair <int, int> my_pair; my_pair = make_pair(1, 2); my_map[my_pair] = 3; // My attempt on iteration for(map<pair<int,int>,int>::iterator it = my_map.begin(); it != my_map.end(); ++it) { cout << it->first << "\n"; } return 0; } ``` How do I have to modify the `cout` line, so that it works?

Original source