Erasing elements in a multimap while iterating

后端 未结 2 1748
醉梦人生
醉梦人生 2020-12-31 09:26

I\'m writing a nodal path finding algorithm. I need to run through a multimap and delete elements out of it under certain conditions, but keep iterating through the multimap

2条回答
  •  孤城傲影
    2020-12-31 10:04

    Won't this be better?

    std::multimap m;
    typedef std::multimap::iterator Iter;¸
    
    for (Iter it = m.begin(); it != m.end(); ) {
        if ( /* some condition */ ) {
            Iter save = it;
            ++it;
            m.erase(save);
        } else
           ++it;
    }
    

提交回复
热议问题