From: ulrich.eckhardt@dominolaser.com   
      
   Am 01.07.2013 09:58, schrieb HungryGoat:   
   > template    
   > inline bool   
    > replace_key(Cont& c,   
   > const typename Cont::key_type& old_key,   
   > const typename Cont::key_type& new_key)   
   > {   
   > typename Cont::iterator pos;   
   > pos = c.find(old_key);   
   > if (pos != c.end()) {   
   > //insert new element with value of old element   
   > c.insert(typename Cont::value_type(new_key,   
   > pos->second));   
   >   
   > //remove old element   
   > c.erase(pos);   
   > return true;   
   > }   
   > else {   
   > //key not found   
   > return false;   
   > }   
   > }   
   >   
   > My question is, when we call c.erase(pos), aren't there chances that   
   > the pos iterator is invalidated by the earlier call to c.insert   
   > function.   
      
   No. In general, map's iterators are only invalidated when the exact   
   element they are referring to is erased. Inserting at a different   
   position doesn't change the validity of other iterators.   
      
   There is one thing that isn't considered here though, and that is what   
   happens when old=new. In that case, inserting the new element fails (it   
   returns false and an iterator to the existing element) after which the   
   old/new element is erased. Effectively, the request to replace something   
   ended with the element being erased, which is probably not what was   
   intended.   
      
    /* locate source element */   
    typename Cont::iterator pos = c.find(old_key);   
    if(pos == c.end())   
    return false;   
      
    /* insert new element with value of old element and   
    erase old element on success */   
    typename Cont::value_type val(new_key, pos->second));   
    if(c.insert(val).second)   
    c.erase(pos);   
      
    return true;   
      
      
   A similar case is what should happen when the target position is   
   different but already occupied, which only the context can answer. I'd   
   also question whether returning false is a good way to signal that the   
   source element is not found, using exceptions might be a better   
   alternative. Technically, the code is fine though and it won't invoke   
   undefined behaviour.   
      
      
   > My impression is that the iterators are invalidated after any insert   
   > or delete operation.   
      
   No, not for map, but other containers have different rules. All these   
   are documented, just search for "iterator invalidation rules".   
      
   Uli   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|