title: Stealing resources from std::map's keys allowed?
body: <p>In C++, is it OK to steal resources from a map that I do not need afterwards anymore? More precisely, assume I have a <code>std::map</code> with <code>std::string</code> keys and I want to construct a vector out of it by stealing the resources of the <code>map</code>s keys using <code>std::move</code>. Note that such a write access to the keys corrupts the internal datastructure (ordering of keys) of the <code>map</code> but I won't use it afterwards. </p>
  
  <p><strong>Question</strong>: Can I do this without any problems or will this lead to unexpected bugs for example in the destructor of the <code>map</code> because I accessed it in a way that <code>std::map</code> was not intended for?</p>
  
  <p>Here is an example program:</p>
  
  <pre><code>#include&lt;map&gt;
  #include&lt;string&gt;
  #include&lt;vector&gt;
  #include&lt;iostream&gt;
  using namespace std;
  int main(int argc, char *argv[])
  {
      std::vector&lt;std::pair&lt;std::string,double&gt;&gt; v;
      { // new scope to make clear that m is not needed 
        // after the resources were stolen
          std::map&lt;std::string,double&gt; m;
          m["aLongString"]=1.0;
          m["anotherLongString"]=2.0;
          //
          // now steal resources
          for (auto &amp;p : m) {
              // according to my IDE, p has type 
              // std::pair&lt;const class std::__cxx11::basic_string&lt;char&gt;, double&gt;&amp;
              cout&lt;&lt;"key before stealing: "&lt;&lt;p.first&lt;&lt;endl;
              v.emplace_back(make_pair(std::move(const_cast&lt;string&amp;&gt;(p.first)),p.second));
              cout&lt;&lt;"key after stealing: "&lt;&lt;p.first&lt;&lt;endl;
          }
      }
      // now use v
      return 0;
  }
  </code></pre>
  
  <p>It produces the output:</p>
  
  <pre><code>key before stealing: aLongString
  key after stealing: 
  key before stealing: anotherLongString
  key after stealing: 
  </code></pre>
  
  <p>EDIT: I would like to do this for the entire content of a large map and save dynamic allocations by this resource stealing.</p>