fork(3) download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. void printMultimap(const std::multimap<int, int>& multiMap)
  5. {
  6. std::cout << "MultiMap:\n";
  7. for (const auto& pair : multiMap)
  8. {
  9. std::cout << pair.first << ":" << pair.second << "\n";
  10. }
  11. }
  12.  
  13. int main()
  14. {
  15. std::multimap<int, int> multiMap;
  16. multiMap.insert(std::make_pair(1, 1));
  17. multiMap.insert(std::make_pair(1, 2));
  18. multiMap.insert(std::make_pair(2, 3));
  19. multiMap.insert(std::make_pair(2, 4));
  20. multiMap.insert(std::make_pair(2, 5));
  21. multiMap.insert(std::make_pair(3, 1));
  22.  
  23. printMultimap(multiMap);
  24.  
  25. auto range = multiMap.equal_range(2);
  26. for (auto iterator = range.first; iterator != range.second;)
  27. {
  28. if (iterator->second != 4)
  29. {
  30. multiMap.insert(std::make_pair(-1, iterator->second));
  31. }
  32. iterator = multiMap.erase(iterator);
  33. }
  34.  
  35. printMultimap(multiMap);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
MultiMap:
1:1
1:2
2:3
2:4
2:5
3:1
MultiMap:
-1:3
-1:5
1:1
1:2
3:1