fork(3) download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. map<int, char> v = {{1, 'a'}, {7, 'd'}};
  8. auto it = v.begin();
  9.  
  10. for (; it != v.end(); it++) {
  11. //cout << "Current it position = " << it - v.begin() << endl;
  12. //auto it2 = it++;
  13.  
  14. // TODO #1: Erasing the last element cause infinitive loop
  15. // (ONLY valid for vector, not map)
  16. if (it->first == 7) v.erase(it);
  17.  
  18. // TODO #2: If not #1, next to deleted element
  19. // is stepped over (not erased)
  20. // (ONLY valid for vector, not map)
  21.  
  22. //cout << "Erased element: " << *it2 << endl;
  23. //v.erase(it2);
  24. }
  25.  
  26. //
  27. cout << "Ended. Map size: " << v.size() << endl;
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Ended. Map size: 1