简体   繁体   中英

Assigning vectors without copying them

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?

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

您可以使用std :: swap交换内容

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM