multimap
Initialization
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. template <class T>
  6. void display(T it, T end)
  7. {
  8. while(it != end)
  9. {
  10. std::cout<<it->first<<" "<<it->second<<std::endl;
  11. std::advance(it, 1);
  12. }
  13. }
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. std::multimap<std::string, std::string> m;
  18. m.insert(std::make_pair("Buick", "Centry"));
  19. m.insert(std::make_pair("Buick", "Rendezvous"));
  20.  
  21. display(m.begin(), m.end());
  22.  
  23. return 0;
  24. }
Access Elements
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. template <class T>
  6. void display(T it, T end)
  7. {
  8. while(it != end)
  9. {
  10. std::cout<<it->first<<" "<<it->second<<std::endl;
  11. std::advance(it, 1);
  12. }
  13. }
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. std::multimap<std::string, std::string> m;
  18. m.insert(std::make_pair("Buick", "Centry"));
  19. m.insert(std::make_pair("Buick", "Rendezvous"));
  20. m.insert(std::make_pair("Honda", "CRV"));
  21. m.insert(std::make_pair("Honda", "Accord"));
  22.  
  23. //equal_range
  24. auto ret = m.equal_range("Honda");
  25. for(auto it = ret.first; it != ret.second; ++it)
  26. std::cout<<it->second<<std::endl;
  27.  
  28. return 0;
  29. }
Find and Erase
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. template <class T>
  6. void display(T it, T end)
  7. {
  8. while(it != end)
  9. {
  10. std::cout<<it->first<<" "<<it->second<<std::endl;
  11. std::advance(it, 1);
  12. }
  13. }
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. std::multimap<std::string, std::string> m;
  18. m.insert(std::make_pair("Buick", "Centry"));
  19. m.insert(std::make_pair("Buick", "Rendezvous"));
  20. m.insert(std::make_pair("Honda", "CRV"));
  21. m.insert(std::make_pair("Honda", "Accord"));
  22.  
  23. //find
  24. auto it = m.find("Honda");
  25. std::cout<<it->first<<" "<<it->second<<std::endl;//Honda CRV
  26.  
  27. //erase
  28. m.erase(it);
  29.  
  30. display(m.begin(), m.end());
  31.  
  32. return 0;
  33. }
Multimap with Objects
  1. #include <iostream>
  2. #include <map>
  3. #include "Rectangle.h"
  4.  
  5. struct comp{
  6. bool operator()(const Rectangle &a, const Rectangle &b)
  7. {
  8. return (a.getArea() < b.getArea());
  9. }
  10. };
  11.  
  12. template <class T>
  13. void display(T it, T end)
  14. {
  15. while(it != end)
  16. {
  17. std::cout<<it->first.getArea()<<std::endl;
  18. std::advance(it, 1);
  19. }
  20. }
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24. std::multimap<Rectangle, int, comp> m;
  25. Rectangle r1(1, 2);
  26. Rectangle r2(1, 2);
  27. Rectangle r3(2, 3);
  28. Rectangle r4(2, 4);
  29.  
  30. m.insert(std::make_pair(r1, 1));
  31. m.insert(std::make_pair(r2, 2));
  32. m.insert(std::make_pair(r3, 3));
  33. m.insert(std::make_pair(r4, 4));
  34.  
  35. Rectangle r5(1, 2);
  36. auto it = m.find(r5);
  37. std::cout<<it->first.getArea()<<std::endl;//2
  38.  
  39. display(m.begin(), m.end());
  40.  
  41. return 0;
  42. }
Reference