fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <iterator>
  4. using namespace std;
  5.  
  6. int main() {
  7. map<int,int> mymap = {{1,0},{2,1},{9,2},{10,3},{11,4}};
  8. for(auto it = mymap.rbegin(); it != mymap.rend(); ){
  9. auto v = --(it.base());
  10. cout << "erasing: " << v->first << endl;
  11. v = mymap.erase(v);
  12. it = map<int,int>::reverse_iterator(v);
  13. }
  14. return 0;
  15. }
Success #stdin #stdout 0s 5548KB
stdin
Standard input is empty
stdout
erasing: 11
erasing: 10
erasing: 9
erasing: 2
erasing: 1