c ++移动后重新分配

c++ reassign after moved

据我了解,从其他帖子中移出后,该对象处于有效但未指定的状态。我可以将事物重新分配给向量吗,如下例所示:

std::vector a = {1,2,3};
std::vector b = std::move(a);
a = {1,3,4}

是否保证a终究会包含1,3,4

这是有保证的。移动后,vector 保证为空,即没有元素。被其他内容赋值就好了

After the move, other is guaranteed to be empty().

当然,a会包含{1,3,4}。简单地从向量移动意味着获取它的内容,动态分配的数组,并将该内容提供给 left-hand 旁边的东西。现在分配将为旧向量提供新内容。这是完全正确的。