title: C++ Storing references to values in std::map
body: <p>Am I right in assuming that adding/removing elements to an std::map does not effect the other elements (ie cause them to be relocated in memory) and so that the following is safe:</p>
  
  <p>I looked at various sites with info on the container but only found out about the cases where iterators are invalidated, which I already know...</p>
  
  <pre><code>std::map&lt;std::string,std::string&gt; map;
  PopulateMap(map);
  std::string &amp;a= map["x"];
  AddMoreData(map);
  RemoveRandomKeysExceptX(map);
  map["x"] = "foo";
  std::cout &lt;&lt; a &lt;&lt; " " &lt;&lt; map["x"] &lt;&lt; std::endl;//prints "foo foo"
  a = "bar";
  std::cout &lt;&lt; a &lt;&lt; " " &lt;&lt; map["x"] &lt;&lt; std::endl;//prints "bar bar"
  </code></pre>
  
  <p>I tested some similar code on VC9, which seems to work however that doesn't mean I didn't just get lucky or that it doesn't vary across compilers.</p>