title: Is it more efficient to copy a vector by reserving and copying, or by creating and swapping?
body: <p>I am trying to efficiently make a copy of a vector. I see two possible approaches:</p>
  <pre><code>std::vector&lt;int&gt; copyVecFast1(const std::vector&lt;int&gt;&amp; original)
  {
    std::vector&lt;int&gt; newVec;
    newVec.reserve(original.size());
    std::copy(original.begin(), original.end(), std::back_inserter(newVec));
    return newVec;
  }
  
  std::vector&lt;int&gt; copyVecFast2(std::vector&lt;int&gt;&amp; original)
  {
    std::vector&lt;int&gt; newVec;
    newVec.swap(original);
    return newVec;
  }
  </code></pre>
  <p>Which of these is preferred, and why? I am looking for the most efficient solution that will avoid unnecessary copying.</p>