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;
    
    while(erased==false){
       temp_e = map[i];
       if(temp_e!=NULL){
            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);
                map.erase(itr);
    
                erased = true;
            }
        }
        i--;
    
    }}
    

    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);
    if(iter!=map.end())
    map.erase(itr);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.