Is it more efficient to copy a vector by reserving and copying, or by creating and swapping? [duplicate]

Your second example does not work if you send the argument by reference. Did you mean

void copyVecFast(vec<int> original) // no reference
{

  vector<int> new_;
  new_.swap(original); 
}

That would work, but an easier way is

vector<int> new_(original);

Leave a Comment