daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
I've read around, some say
vector.swap ( otherVector );
is fastest?
Typically, but that's not a copy operation.Let me know if there are fast ways to do this please!
If performance is an issue, it's often better to think squiggly. Instead of making a copy, can get you get away with straight aliasing? You might not need to do any copying at all.
Another option is lazy copying such that only the modified elements of the second vector are copied. Unless all of the elements are going to be modified, lazy methods will save both time and space.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
What about the vector class' built-in copy constructor? I don't know exactly how it works, but the STL is usually pretty quick...
vector<type> copyVector(originalVector);
Or the built-in assignment operator?
vector<int> vecOne(10,0);
vector<int> vecTwo(20,0);
vecOne = vecTwo;
Both of these, of course, would require the the dataType stored in the vector have a proper copy constructor defined (which a float should). That's some massive memory usage though. Is it really that necessary?
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
>What about the vector class' built-in copy constructor? I don't
>know exactly how it works, but the STL is usually pretty quick...
It works about how you would expect it to work. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401