fork download
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5. void printMap(const std::map<int, double>& m)
  6. {
  7. for (auto kv : m)
  8. {
  9. std::cout << '{' << kv.first << ", " << kv.second << '}';
  10. }
  11. std::cout << '\n';
  12. }
  13.  
  14. int main() {
  15. std::cout << "erase by iterator:\n";
  16. std::map<int, double> m1 = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 } };
  17. printMap(m1);
  18. m1.erase(m1.find(2));
  19. printMap(m1);
  20.  
  21. std::cout << "erase by key:\n";
  22. std::map<int, double> m2 = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 } };
  23. printMap(m2);
  24. m2.erase(2);
  25. printMap(m2);
  26. return 0;
  27. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
erase by iterator:
{1, 1.1}{2, 2.2}{3, 3.3}
{1, 1.1}{3, 3.3}
erase by key:
{1, 1.1}{2, 2.2}{3, 3.3}
{1, 1.1}{3, 3.3}