Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Erasing Elements from a map, but position not erased

Erasing Elements from a map, but position not erased

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 662 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    droelf
    wrote on last edited by
    #1

    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 :/

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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);
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved