multmap.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // multmap.cpp -- use a multimap
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <algorithm>
  6. typedef int KeyType;
  7. typedef std::pair<const KeyType, std::string> Pair;
  8. typedef std::multimap<KeyType, std::string> MapCode;
  9. int main()
  10. {
  11. using namespace std;
  12. MapCode codes;
  13. codes.insert(Pair(415, "San Francisco"));
  14. codes.insert(Pair(510, "Oakland"));
  15. codes.insert(Pair(718, "Brooklyn"));
  16. codes.insert(Pair(718, "Staten Island"));
  17. codes.insert(Pair(415, "San Rafael"));
  18. codes.insert(Pair(510, "Berkeley"));
  19. cout << "Number of cities with area code 415: "
  20. << codes.count(415) << endl;
  21. cout << "Number of cities with area code 718: "
  22. << codes.count(718) << endl;
  23. cout << "Number of cities with area code 510: "
  24. << codes.count(510) << endl;
  25. cout << "Area Code City\n";
  26. MapCode::iterator it;
  27. for (it = codes.begin(); it != codes.end(); ++it)
  28. cout << " " << (*it).first << " "
  29. << (*it).second << endl;
  30. pair<MapCode::iterator, MapCode::iterator>
  31. auto range
  32. = codes.equal_range(718);
  33. cout << "Cities with area code 718:\n";
  34. for (it = range.first; it != range.second; ++it)
  35. cout << (*it).second << endl;
  36. // cin.get();
  37. return 0;
  38. }