Congratulations to our 2022 Qt Champions!

Erasing Elements from a map, but position not erased

  • So i want to erase a specific Element from my map with this:

    void functionloader::erase_edge_from_map(edge* e,emap& map){
      bool erased = false;
      emap::iterator itr;
    int i = 0;
       temp_e = map[i];
            if(((temp_e->u->name == e->u->name)&&(temp_e->v->name == e->v->name)) ||
               ((temp_e->u->name == e->v->name)&&(temp_e->v->name == e->u->name))){
                itr = map.find(i);
                erased = true;

    But when i for example erase map[5] it erases it, but in a further iteration the position with this key appears again but
    without an element.

    Someone knows where the problem is? Or even better would be another way to erase elements from my map :/

  • if you call map[n] it will create an element if it's not contained. use map.value(n) instead.
    If you don't care about iterators becoming invalid after the removal use QMap::remove().
    otherwise use

    itr = map.find(i);

Log in to reply