fork download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. using namespace std;
  5.  
  6. void print(map<int,int>& s)
  7. {
  8. cout << "\nmp = {";
  9. for(auto p = s.begin(); p!= s.end();)
  10. cout << p->first << "->" << p->second << ",}"[++p==s.end()];
  11. }
  12.  
  13. void print(multimap<int,int>& s)
  14. {
  15. cout << "\nmmp = {";
  16. for(auto p = s.begin(); p!= s.end();)
  17. cout << p->first << "->" << p->second << ",}"[++p==s.end()];
  18. }
  19.  
  20. int main()
  21. {
  22. map<int,int> s;
  23. s[1]=1, s[2]=4, s[3]=9, s[4]=16, s[5]=25, s[6]=36, s[7]=49;
  24. print(s);
  25. map<int,int> s2 = {{1,11},{2,12},{3,13}};
  26. print(s2);
  27. s.erase(3);
  28. print(s);
  29. map<int,int>::iterator ix = s.find(4);
  30. s.erase(ix);
  31. print(s);
  32. s.erase(s.find(5), s.find(9));
  33. print(s);
  34. cout << "\nCount of 1: " << s.count(1);
  35. cout << "\nCount of 2: " << s.count(2);
  36. print(s);
  37.  
  38. auto it = s.lower_bound(5);
  39. cout << "\nThe lower bound of 5 is " << it->first << ".";
  40.  
  41. auto it_pair = s.equal_range(1);
  42. cout << "\nThe bounds of 1 are " << it_pair.first->first << " and " << it_pair.second->first << ".";
  43.  
  44. multimap<int,int> ms;
  45. ms.insert({1,1});
  46. ms.insert({2,4});
  47. ms.insert({2,9});
  48. ms.insert({2,16});
  49. ms.insert({5,25});
  50. print(ms);
  51.  
  52. it_pair = ms.equal_range(2);
  53. ms.erase(it_pair.first, it_pair.second);
  54. print(ms);
  55. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
mp = {1->1,2->4,3->9,4->16,5->25,6->36,7->49}
mp = {1->11,2->12,3->13}
mp = {1->1,2->4,4->16,5->25,6->36,7->49}
mp = {1->1,2->4,5->25,6->36,7->49}
mp = {1->1,2->4}
Count of 1: 1
Count of 2: 1
mp = {1->1,2->4}
The lower bound of 5 is 2.
The bounds of 1 are 1 and 2.
mmp = {1->1,2->4,2->9,2->16,5->25}
mmp = {1->1,5->25}