Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning vectors without copying them

Tags:

c++

c++11

vector

I have two std::vector with several std::unordered_set inside. However, one of the vector will be replacing the other throughout the execution, like this:

vector1 = ...
vector2 = ...
// some operations
vector1 = vector2
vector2 = std::vector<...>

Is there a way of achieving this without having to copy the contents of the vectors?

like image 460
Hel Avatar asked Oct 22 '25 01:10

Hel


1 Answers

Since C++11 you can move assign them:

vector1 = std::move(vector2); // move vector2 to vector1
vector2 = std::vector<...>;   // move the temporary vector to vector2
like image 126
songyuanyao Avatar answered Oct 24 '25 14:10

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!