Is it possible to transfer ownership of a vector contents from one vector to another?
vector<T> v1;
// fill v1
vector<T> v2 = OvertakeContents(v1);
// now v1 would be empty and v2 would have all the contents of v1
It is possible for lists with splice function. This should be possible in constant time for whole vector as well.
If it isn't then why not?
Check out std::swap
vector<T> v1;
// fill v1
vector<T> v2;
swap(v1, v2);
OR
v2.swap(v1);
Swap Reference
std::vector has a swap() function that works pretty much like this.
vector<T> v2;
v2.swap(v1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With