将元素从 std::vector 移动到另一个

Moving elements from std::vector to another one(将元素从 std::vector 移动到另一个)
本文介绍了将元素从 std::vector 移动到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何将某些元素从第一个向量移动到第二个向量,而这些元素将从第一个向量中删除?
如果我使用 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 移动到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)