fork download
  1. // unordered_map::find
  2. #include <iostream>
  3. #include <string>
  4. #include <unordered_map>
  5. using namespace std;
  6.  
  7. typedef std::unordered_map<std::string,double> mymap_t;
  8.  
  9. std::ostream& operator << (std::ostream& stream, mymap_t::const_iterator& it)
  10. {
  11. return stream << it->first << ":" << it->second << endl;
  12. }
  13.  
  14. int main ()
  15. {
  16. mymap_t mymap;
  17. mymap["mom"] = 5.4;
  18. mymap["dad"] = 6.1;
  19. mymap["bro"] = 5.9;
  20.  
  21. std::string input;
  22. std::cout << "who? ";
  23. getline (std::cin,input);
  24.  
  25. mymap_t::const_iterator got = mymap.find (input);
  26.  
  27. cout << got <<endl;
  28.  
  29. if ( got == mymap.end() )
  30. std::cout << "not found";
  31. else
  32. std::cout << got->first << " is " << got->second;
  33.  
  34. std::cout << std::endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 2992KB
stdin
mom
stdout
who? mom:5.4

mom is 5.4