How can I delete elements of a std::map with an iterator?

前端 未结 3 1077
孤城傲影
孤城傲影 2020-12-13 03:35

I would like to loop through an std::map and delete items based on their contents. How best would this be done?

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 04:18

    for(MyMap::iterator it = mymap.begin(); it!=mymap.end(); ) {
      if(mycondition(it))
        it = mymap.erase(it);
      else
        it++;
    }
    

    edit: seems that this works in MSVC only

    edit2: in c++0x this works for associative containers too

提交回复
热议问题