vector.emplace_back()和vector.push_back()是否做相同的事情? [英] Do vector.emplace_back() and vector.push_back() do the same thing?

查看:114
本文介绍了vector.emplace_back()和vector.push_back()是否做相同的事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图将整数添加到向量的后面,并错误地认为 push_back()将新数据添加到向量的前面(也称为vector [0]).我在Xcode中进行了测试,并针对 emplace_back()测试了 push_back(),并得到了相同的结果.我以为它们是不同的,但这使我认为也许他们做同样的事情.如果是这样,为什么vector有不同的方法?

So I was trying to add integers onto the back of my vector and mistakenly thought push_back() added the new data onto the front of the vector (aka vector[0]). I did a test in Xcode and tested push_back() against emplace_back() and got the same results. I thought they were different, but this makes me think that maybe they do the same thing. If this is so, why does vector have the different methods?

这是我的代码,以防万一:

Here's my code in case I was doing:

#include <vector>
#include <iostream> 

using namespace std ;

int main(int argc, const char * argv[])
{
    // for push_back
    vector<int> push;
    push.push_back(1);
    push.push_back(2);
    push.push_back(3);
    push.push_back(4);
    push.push_back(5);
    push.push_back(6);
    //display push_back
    for (int i = 0; i < push.size(); i++) {
        cout << "push[" << i  << "]: " << push[i] << endl;
    }
    // distance between the two funcitons
    cout << endl << endl;

    vector<int> emplace;
    emplace.emplace_back(1);
    emplace.emplace_back(2);
    emplace.emplace_back(3);
    emplace.emplace_back(4);
    emplace.emplace_back(5);
    emplace.emplace_back(6);

    //display emplace_back
    for (int i = 0; i < emplace.size(); i++) {
        cout << "emplace[" << i  << "]: " << emplace[i] << endl;
    }
        return 0;
}

返回是:

push[0]: 1
push[1]: 2
push[2]: 3
push[3]: 4
push[4]: 5
push[5]: 6


emplace[0]: 1
emplace[1]: 2
emplace[2]: 3
emplace[3]: 4
emplace[4]: 5
emplace[5]: 6

我知道这是一个非常简单的问题,但是我只是想确保自己没有做一些愚蠢的错误并误解了向量类的功能.

I know this is a super easy question, but I just want to make sure I am not doing something stupidly wrong and misunderstanding the abilities of the vector class.

推荐答案

区别在于 emplace_back 将在原位构造对象,而不是复制或移动

The difference is that emplace_back will construct the object in place as opposed to copying or moving, cppreference section on std::vector::emplace_back says:

通常使用newplacement在容器提供的位置就位构造元素.参数args ...被转发给构造函数

which typically uses placement-new to construct the element in-place at the location provided by the container. The arguments args... are forwarded to the constructor

这对于重物而言很重要.我们可以从原始提案:

This matters in the case of heavy objects. We can see that this was the motivation from the original proposal which says:

插入展示位置的动机是容器-特别是容器基于节点的容器—对于存储沉重的文件非常有用对象.在某些环境中,效率非常重要,但通常,如果没有容器,就无法将元素放入容器中复制它们.重物可以直接存储其数据,其中大小写移动语义不会提高复制性能.此外,当效率至关重要时,解决方案不能依赖编译器优化,因为它们是可选的,可能不会在他们是最需要的.插入位置让我们创建一个元素一次,放在我们想要的容器中,再也不需要移动或复制它.通过引入简单而直接的方式新的可变参数函数,其参数将传递给元素的构造函数使用可变参数模板和完美的转发功能.

The motivation for placement insert is that containers—especially node-based containers—are very useful for the storage of heavy objects. In some environments efficiency is very important, but there is in general no way to put elements into containers without copying them. A heavy object may store its data directly, in which case move semantics will not improve copy performance. Furthermore, when efficiency is critical the solution cannot depend on compiler optimizations since they are optional and may not occur where they are most needed. Placement insertion lets us create an element once, in the container where we want it, and never have to move it or copy it. It does so in a simple and direct way by introducing new variadic functions that take arguments that are passed on to the element’s constructor using variadic templates and perfect forwarding.

我们可以举一个例子,它与有效的现代C ++ 第42项:考虑放置而不是插入,它具有以下示例:

We can take an example of were this makes a difference from Effective Modern C++ Item 42: Consider emplacement instead of insertion, which has the following example:

std::vector<std::string> vs; // container of std::string
vs.push_back("xyzzy"); // add string literal

这会导致创建一个临时文件,而不是:

which results in the creation of a temporary as opposed to:

vs.emplace_back("xyzzy");

没有.

这篇关于vector.emplace_back()和vector.push_back()是否做相同的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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