74

I have a map like this:

map<string, pair<string,string> > myMap;

And I've inserted some data into my map using:

myMap.insert(make_pair(first_name, make_pair(middle_name, last_name)));

How can I now print out all the data in my map?

honk
  • 8,101
  • 11
  • 72
  • 75
Zhi Rui
  • 895
  • 1
  • 10
  • 10
  • 2
    See [this question](http://stackoverflow.com/q/4850473/485561). – Mankarse Dec 28 '12 at 14:26
  • You loop through the map using an iterator, take the key, the members of the pair, and apply `cout::operator< –  Dec 28 '12 at 14:28
  • http://stackoverflow.com/questions/1063453/how-to-display-map-contents This question and answer should do it. – Benjamin Trent Dec 28 '12 at 14:28
  • 1
    possible duplicate of [How to loop through a c++ map](http://stackoverflow.com/questions/4844886/how-to-loop-through-a-c-map) – Hogan Dec 28 '12 at 16:02

4 Answers4

109
for(map<string, pair<string,string> >::const_iterator it = myMap.begin();
    it != myMap.end(); ++it)
{
    std::cout << it->first << " " << it->second.first << " " << it->second.second << "\n";
}

In C++11, you don't need to spell out map<string, pair<string,string> >::const_iterator. You can use auto

for(auto it = myMap.cbegin(); it != myMap.cend(); ++it)
{
    std::cout << it->first << " " << it->second.first << " " << it->second.second << "\n";
}

Note the use of cbegin() and cend() functions.

Easier still, you can use the range-based for loop:

for(const auto& elem : myMap)
{
   std::cout << elem.first << " " << elem.second.first << " " << elem.second.second << "\n";
}
Armen Tsirunyan
  • 125,569
  • 56
  • 315
  • 427
29

If your compiler supports (at least part of) C++11 you could do something like:

for (auto& t : myMap)
    std::cout << t.first << " " 
              << t.second.first << " " 
              << t.second.second << "\n";

For C++03 I'd use std::copy with an insertion operator instead:

typedef std::pair<string, std::pair<string, string> > T;

std::ostream &operator<<(std::ostream &os, T const &t) { 
    return os << t.first << " " << t.second.first << " " << t.second.second;
}

// ...
std:copy(myMap.begin(), myMap.end(), std::ostream_iterator<T>(std::cout, "\n"));
mk12
  • 25,294
  • 29
  • 95
  • 136
Jerry Coffin
  • 455,417
  • 76
  • 598
  • 1,067
  • Why is the & required in the above snipped? `for (auto& t : myMap) std::cout << t.first << " " << t.second.first << " " << t.second.second << "\n";` – Manjunath Oct 13 '17 at 18:49
  • 5
    Using a reference `&` avoids making a copy during the loop iteration. – CyprUS Mar 08 '18 at 23:20
25

Since C++17 you can use range-based for loops together with structured bindings for iterating over your map. This improves readability, as you reduce the amount of needed first and second members in your code:

std::map<std::string, std::pair<std::string, std::string>> myMap;
myMap["x"] = { "a", "b" };
myMap["y"] = { "c", "d" };

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

Output:

m[x] = (a, b)
m[y] = (c, d)

Code on Coliru

honk
  • 8,101
  • 11
  • 72
  • 75
0

The easiest way is to declare an iterator first as
map<string ,string> :: iterator it;

and after that print it out by looping over the map using iterator from myMap.begin() to myMap.end() and print out key and value pairs in the map with it->first for key and it->second for value.

  map<string ,string> :: iterator it;
    for(it=myMap.begin();it !=myMap.end();++it)
      {
       std::cout << it->first << ' ' <<it->second;
      }