56705840   
   From: daniel.kruegler@googlemail.com   
      
   On 2011-08-25 01:44, A. McKenney wrote:   
   > I found out today that, for std::map, insertion of elements doesn't   
   > invalidate iterators.   
      
   This is correct.   
      
   > That is, the following code is guarranteed to work (once you add the   
   > right #includes and wrap it in a function.)   
   >   
   > typdef std::map MyMapType;   
   > MyMapType my_map;   
   >   
   > my_map[0] = "element 0";   
   > MyMapType::iterator my_map_it = my_map.find(0);   
   > my_map[1] = "element 1";   
   > std::cout<< my_map_it->second<< std::endl; // should output "element   
   > 0".   
      
   Yep.   
      
   > However, it occurred to me that an iterator is not necessarily just   
   > a pointer to a value_type . It could contain some way to find   
   > elements even if they've been moved. Is the following guarranteed by the   
   > standard to work? That is, does the standard require elements to remain   
   in   
   > the memory location where they were created?   
   >   
   > typdef std::map MyMapType;   
   > MyMapType my_map;   
   >   
   > my_map[0] = "element 0";   
   > MyMapType::iterator my_map_it = my_map.find(0);   
   > std::string&str_0 = my_map_it->second;   
   > my_map[1] = "element 1";   
   > std::cout<< str_0<< std::endl; // should output "element 0".   
      
   This is guaranteed as well, based on 23.2.4 [associative.reqmts] p9:   
      
   "The insert and emplace members shall not affect the validity of   
   iterators and references to the container, and the erase members shall   
   invalidate only iterators and references to the erased elements."   
      
   In short, the standard guarantees that both iterators *and* references   
   remain valid.   
      
   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)   
|