[英]Assigning vectors without copying them
I have two std::vector
with several std::unordered_set
inside. 我有两个
std::vector
,里面有几个std::unordered_set
。 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: 从C ++ 11开始,您可以移动分配它们:
vector1 = std::move(vector2); // move vector2 to vector1
vector2 = std::vector<...>; // move the temporary vector to vector2
您可以使用std :: swap交换内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.