5

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?

honk
  • 9,137
  • 11
  • 75
  • 83
Andrej
  • 3,719
  • 11
  • 44
  • 73

5 Answers5

5

it->first is an object of type const std::pair<int, int> that is it is the key. it->second is an object of type int that is it is the mapped value. If you want simply to output the key and the mapped value you could write

for ( map<pair<int,int>,int>::iterator it = my_map.begin(); it != my_map.end(); ++it ) 
{
    cout << "( " << it->first.first << ", " 
                 << it->first.second 
                 << " ): "
                 << it->second
                 << std::endl;
}

Or you could use the range-based for statement

for ( const auto &p : my_map )
{
    cout << "( " << p.first.first << ", " 
                 << p.first.second 
                 << " ): "
                 << p.second
                 << std::endl;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
3

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

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
2

Do you have access to c++11, the auto keyword can make it look a lot nicer. I compiled and ran this fine.

#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(auto it = my_map.begin(); it != my_map.end(); ++it) {
        cout << it->first.first << "\n";
      }

      return 0;
    }
marsh
  • 2,592
  • 5
  • 29
  • 53
2

By default, std::ostream does not know how to deal with std::pair. So, if you want to print your keys, the easiest way is to directly access their two elements, as also shown in the other answers. However, if you have access to C++11 features, then you can also use a range-based for loop for iterating over your map as follows:

int main() {
    std::map<std::pair<int, int>, int> my_map{ {{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9} };

    for (auto const &kv : my_map)
        std::cout << "(" << kv.first.first << ", " << kv.first.second << ") = " << kv.second << std::endl;

    return 0;
}

Output:

(1, 2) = 3
(4, 5) = 6
(7, 8) = 9

If you can use C++17, then you can further shorten the code by using structured binding in the range-based for loop as follow:

for (auto const &[k, v] : my_map)
    std::cout << "(" << k.first << ", " << k.second << ") = " << v << std::endl;

Code on Coliru

honk
  • 9,137
  • 11
  • 75
  • 83
1
#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.first << "\n";
        cout << it->first.second << "\n";
    }

    return 0;
}
user2970916
  • 1,146
  • 3
  • 15
  • 37