Moving elements from std::vector to another one

前端 未结 4 2155
故里飘歌
故里飘歌 2020-11-27 15:55

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 fro

4条回答
  •  甜味超标
    2020-11-27 16:16

    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.

    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.

提交回复
热议问题