From: daniel.kruegler@googlemail.com   
      
   On 2013-07-01 09:58, HungryGoat wrote:   
   > Hi, This code is from "The C++ standard library" by Nicolai book,   
   > page no. 178   
   >   
   > 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.   
      
   > My impression is that the iterators are invalidated after any insert   
   > or delete operation.   
      
   Associative container provide special guarantees in regard to iterator   
   invalidation. The relevant wording is 23.2.4 [associative.reqmts] p9:   
      
   "The insert and emplace members shall not affect the validity of   
   iterators and references to the container, [..]"   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
      
   --   
    [ 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)   
|