home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,400 of 33,346   
   A. McKenney to All   
   std::map -- do pointers/refs to elements   
   24 Aug 11 16:44:20   
   
   56705840   
   From: alan_mckenney1@yahoo.com   
      
   I found out today that, for std::map, insertion of elements doesn't   
   invalidate iterators.   
   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".   
      
      
   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".   
      
      
   --   
         [ 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)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca