unordered_map print iterator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// unordered_map::find
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int main ()
{
  std::unordered_map<std::string,double> mymap;
  mymap["mom"] = 5.4;
  mymap["dad"] = 6.1;
  mymap["bro"] = 5.9;

  std::string input;
  std::cout << "who? ";
  getline (std::cin,input);

  std::unordered_map<std::string,double>::const_iterator got = mymap.find (input);

cout << mymap.find <<endl;
cout << got <<endl;
cout << mymap.end <<endl;

  if ( got == mymap.end() )
    std::cout << "not found";
  else
    std::cout << got->first << " is " << got->second;

  std::cout << std::endl;

  return 0;
}




I want to ask why cant i print what are the contents store inside got,and mymap.find will return what,and also mymap.end
Last edited on
why cant i print what are the contents store inside got
got is an iterator. You're probably not interested in what's inside got as it won't mean anything to you. At any rate, there isn't a standard stream operator for it.

If you really want to see what's inside got, you'll need to run your program in a debugger and inspect the data structure. At least there, you'll be able to follow pointers and so on.

It only makes sense to check against mymap.end(), to deference it and increment it (and of course intialise it).
Last edited on
Topic archived. No new replies allowed.