title: Is it safe to store a pointer to an item in an std::set?
body: <p>Is it possible to store a pointer to an element inside of an std::set?  </p>
  
  <p>For instance take the following unsafe example...</p>
  
  <pre><code>std::vector&lt;int&gt; vec;
  //add a bunch of items
  int* ptr = &amp;vec[10];
  //add more items
  std::cout &lt;&lt; *ptr &lt;&lt; std::endl;
  </code></pre>
  
  <p>In this case the memory which ptr points to could have been invalidated by adding extra elements to the vector causing it to reallocate.  However if I had used a linked list instead of a vector I believe this would have been safe since it does not need to reallocate the nodes.</p>
  
  <p>I want to use an std::set to save memory when dealing with redundant strings.  Would the following example be safe?  I think it would be for std::set but not for std::unordered_set.</p>
  
  <pre><code>const char* makeString(const char* s)
  {
    static std::set&lt;std::string&gt; strings_pool;
    return strings_pool.insert(s).first-&gt;c_str();
  }
  </code></pre>
  
  <p>If the string c is not already in the strings_pool it is inserted otherwise it returns an iterator to the string already in the pool.  In either case I get the value of the iterator and return the pointer to underlying cstring.  I think that this is a safe operation but can someone confirm it.</p>
  
  <p>At this link <a href="http://en.cppreference.com/w/cpp/container/set/insert" rel="nofollow noreferrer">http://en.cppreference.com/w/cpp/container/set/insert</a> it says  "No iterators or references are invalidated."  I think this means I can do it.  </p>
  
  <p>Also under the documentation for std::unordered_set it says "References are not invalidated."  Does this means it is safe to use std::unordered_set as well?</p>