C++ 随向量移动::push_back

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11572669/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:17:42  来源:igfitidea点击:

Move with vector::push_back

c++c++11

提问by ggg

Suppose I have the following code:

假设我有以下代码:

#include <vector>
struct A {
    int a;
    int x;
};
int main() {
    using namespace std;
    A a1;
    A a2;
    vector<A> va;
    va.push_back(a1);
    va.push_back(move(a2));
}

I am aware that the elements of std::vector are stored contiguously, unlike a std::list. In the above code a2is moved but is there really no copying of a2to the vector va? What is the difference between va.push_back(a2);and va.push_back(move(a2));?

我知道 std::vector 的元素是连续存储的,这与 std::list 不同。上面的代码a2被移动了,但真的没有复制a2到向量va吗?va.push_back(a2);和 和有va.push_back(move(a2));什么区别?

回答by cdhowie

In your case, there is no effective difference, since you are using compiler-provided copy constructors. You would see a noticeable performance difference when using objects that are move-constructible, and take a lot of effort to copy. In that case, using push_back(x)would create a copy of the object, while push_back(move(x))would tell push_back()that it may "steal" the contents of x, leaving xin an unusable and undefined state.

在您的情况下,没有有效的区别,因为您使用的是编译器提供的复制构造函数。使用可移动构造的对象时,您会看到明显的性能差异,并且需要花费大量精力进行复制。在这种情况下, usingpush_back(x)将创建对象的副本,同时push_back(move(x))会告诉push_back()它可能“窃取” 的内容x,从而x处于无法使用和未定义的状态。

Consider if you had a vector of lists (std::vector<std::list<int> >) and you wanted to push a list containing 100,000 elements. Without move(), the entire list structure and all 100,000 elements will be copied. With move(), some pointers and other small bits of data get shuffled around, and that's about it. This will be lots faster, and will require less overall memory consumption.

考虑一下您是否有一个列表向量 ( std::vector<std::list<int> >) 并且您想要推送一个包含 100,000 个元素的列表。如果没有move(),整个列表结构和所有 100,000 个元素将被复制。使用move(),一些指针和其他少量数据会被打乱,仅此而已。这会快很多,并且需要更少的整体内存消耗。

回答by ForEveR

When you use va.push_back(a2)version vector<T>::push_back(const T&)will be called, when you use va.push_back(move(a2))version vector<T>::push_back(T&&)will be called...

当你使用va.push_back(a2)version 时vector<T>::push_back(const T&)会被调用,当你使用va.push_back(move(a2))version 时vector<T>::push_back(T&&)会被调用...

But in your case there is no difference for perfomance, since

但是在您的情况下,性能没有区别,因为

15 The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members.

15 非联合类 X 的隐式定义的复制/移动构造函数执行其基类和成员的成员复制/移动。

Paragraph 12.8 n3337 draft.

n3337 草案第 12.8 段。

回答by LyingOnTheSky

I want to note something that other answers didn't have gone over; is that the ?.push_back(move(?))will be slower than ?.push_back(?)in your case (when you have trivially copyable objects), because move constructor need to zero\set the moved object, which effectively you are writing\copying two objects.

我想指出一些其他答案没有涉及的内容;是?.push_back(move(?))将比?.push_back(?)您的情况慢(当您具有可简单复制的对象时),因为移动构造函数需要将移动的对象归零\设置,这实际上是您正在编写\复制两个对象。