将元素从std :: vector移动到另一个 [英] Moving elements from std::vector to another one

查看:1383
本文介绍了将元素从std :: vector移动到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一些元素从第一个向量移动到第二个元素,元素会从第一个向量中移除?

如果我使用 std :: move ,元素不从第一个向量中删除。

这是我写的代码:

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:

   move(xSpaces1.begin() + 7, xSpaces1.end(), back_inserter(xSpaces2));


推荐答案

std :: move 允许您移动对象,而不是复制它们,允许更快的执行速度。当移动一定范围的值时,节省的可能会更大。但是,当您从容器移动范围时,容器仍会保留曾经被占用过的地方通过这些值。

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 range from a container, the container still holds the places that were once occupied by these values.

您需要手动调整容器大小以删除这些占位符,如果你想摆脱它们(你不必,万一你会优选将这些容器点用于其他元件)。一种方法是调用 vector :: erase 与您移出容器的范围相同。

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.

这篇关于将元素从std :: vector移动到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆