C++ 将元素从 std::vector 移动到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15004517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving elements from std::vector to another one
提问by user1544067
How can I move some elements from first vector to second, and the elements will remove from the first?
if I am using std::move
, the elements not removed from first vector.
this is the code I wrote:
如何将某些元素从第一个向量移动到第二个向量,并且元素将从第一个向量中删除?
如果我正在使用std::move
,则不会从第一个向量中删除元素。
这是我写的代码:
move(xSpaces1.begin() + 7, xSpaces1.end(), back_inserter(xSpaces2));
采纳答案by dasblinkenlight
The std::move
lets you move the objects, as opposed to copying them, allowing for a potentially faster execution speed. The savings may be even greater when you move a range of values. However, when you do move a rangefrom a container, the container still holds the places that were once occupied by these values.
这std::move
使您可以移动对象,而不是复制它们,从而提高潜在的执行速度。当您移动一系列值时,节省可能更大。但是,当您从容器中移动范围时,容器仍会保留曾经被这些值占用的位置。
You need to resize the container manually to remove these placeholders if you want to get rid of them (you don't have to, in case you would prefer reusing these container spots for other elements). One way to do it is to call vector::erase
on the same range that you moved out of the container.
如果您想删除这些占位符,您需要手动调整容器的大小以删除它们(您不必这样做,以防您希望将这些容器点重用于其他元素)。一种方法是调用vector::erase
您移出容器的相同范围。
回答by Kerrek SB
std::move
and std::copy
operate on elements, not containers. You have to mutate the container separately. For example, to move the first 17 elements of v1
into a new vector v2
:
std::move
并std::copy
操作元素,而不是容器。您必须单独改变容器。例如,要将 的前 17 个元素移动v1
到新向量中v2
:
std::vector<Foo> v1, v2;
// populate v1 with at least 17 elements...
auto it = std::next(v1.begin(), 17);
std::move(v1.begin(), it, std::back_inserter(v2)); // ##
v1.erase(v1.begin(), it);
After line ##
, the first 17 elements of v1
are still there, but they've been "moved-from", so they're in an indeterminate state.
在 line 之后##
, 的前 17 个元素v1
仍然存在,但它们已被“移出”,因此它们处于不确定状态。
回答by Miljen Mikic
Resurrecting an old thread, but I am surprised that nobody mentioned std::make_move_iterator
combined with insert
. It has the important performance benefit of preallocating space in the target vector:
复活一个旧线程,但我很惊讶没有人提到std::make_move_iterator
与insert
. 它具有在目标向量中预分配空间的重要性能优势:
v2.insert(v2.end(), std::make_move_iterator(v1.begin() + 7),
std::make_move_iterator(v1.end()));
As others have pointed out, first vector v1
is now in indeterminate state, so use erase
to clear the mess:
正如其他人所指出的,第一个向量v1
现在处于不确定状态,所以使用erase
来清除混乱:
v1.erase(v1.begin() + 7, v1.end());
回答by alestanis
You can't move elements from one vector to another the way you are thinking about; you will always have to erasethe element positions from the first vector.
您无法按照您的想法将元素从一个向量移动到另一个向量;您将始终必须从第一个向量中删除元素位置。
If you want to change all the elementsfrom the first vector into the second and vice versa you can use swap.
如果要将第一个向量中的所有元素更改为第二个向量,反之亦然,则可以使用swap。
If you want to move the same amount of elementsbetween two vectors, you can use swap_ranges
如果要在两个向量之间移动相同数量的元素,可以使用swap_ranges