title: How to shrink-to-fit an std::vector in a memory-efficient way?
body: <p>I would like to 'shrink-to-fit' an <code>std::vector</code>, to reduce its capacity to its exact size, so that additional memory is freed. The standard trick seems to be the one described <a href="https://stackoverflow.com/questions/1536753/does-stdvector-pop-back-change-vectors-capacity">here</a>:</p>
  
  <pre><code>template&lt; typename T, class Allocator &gt;
  void shrink_capacity(std::vector&lt;T,Allocator&gt;&amp; v)
  {
     std::vector&lt;T,Allocator&gt;(v.begin(),v.end()).swap(v);
  }
  </code></pre>
  
  <p>The whole point of shrink-to-fit is to save memory, but doesn't this method first create a deep copy and then swaps the instances? So at some point -- when the copy is constructed -- the memory usage is doubled? </p>
  
  <p>If that is the case, is there a more memory-friendly method of shrink-to-fit? (In my case the vector is really big and I cannot afford to have both the original plus a copy of it in memory at any time.)</p>