C++11_emplace_back的优点

视频教程:https://www.bilibili.com/video/av86157003

emplace_back的特点

  • 当调用push_back或insert成员函数时,是把元素类型的对象传递给它们,这些对象被拷贝到容器中。而当我们调用一个emplace系列函数时,则是将相应参数传递给元素类型的构造函数。
  • 这样emplace_back能就地通过参数构造对象,不需要拷贝操作,相比push_back能更好的避免内存的拷贝和移动,提升容器插入元素的性能。
  • 大多数情况都应该使用emplace系列函数:emplace; emplace_back; emplace_hit; emplace_fornt; emplace_after

emplace系列函数需要有对应的构造函数

  • emplace函数需要对应的参数对象有对应的构造函数,不然编译报错
  • emplace函数在容器中直接构造元素。传递给emplace函数的参数必须与元素类型的构造函数相匹配
#include<iostream>
#include<list>


struct A
{
    int x;
    double y;

    A()
    {
        x = 0;
        y = 1.0;
    }

    A(int a, double b)
    {
        x = a;
        b = y;
    }
};

int main()
{
    std::list<A> listSample;
    listSample.emplace_back(1, 2.0);
    std::cout << listSample.size() << std::endl;
    return 0;
}

emplace_back和push_back的简单比较

///////////////////////////////////////////////////////
// reference: http://en.cppreference.com/w/cpp/container/vector/emplace_back
namespace {
    struct President {
        std::string name;
        std::string country;
        int year;


        President(std::string p_name, std::string p_country, int p_year)
            : name(std::move(p_name)), country(std::move(p_country)), year(p_year)
        {
            std::cout << "I am being constructed.\n";
        }
        President(President&& other)
            : name(std::move(other.name)), country(std::move(other.country)), year(other.year)
        {
            std::cout << "I am being moved.\n";
        }
        President& operator=(const President& other) = default;
    };
}


int emplacevspush()
{
    std::cout << "test_emplace_2()" << std::endl;
    /*
    The following code uses emplace_back to append an object of type President to a std::vector.
    It demonstrates how emplace_back forwards parameters to the President constructor and shows
    how using emplace_back avoids the extra copy or move operation required when using push_back.
    */
    std::vector<President> elections;
    std::cout << "emplace_back:\n";
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);


    std::vector<President> reElections;
    std::cout << "\npush_back:\n";
    reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));


    std::cout << "\nContents:\n";
    for (President const& president : elections) {
        std::cout << president.name << " was elected president of "
            << president.country << " in " << president.year << ".\n";
    }
    for (President const& president : reElections) {
        std::cout << president.name << " was re-elected president of "
            << president.country << " in " << president.year << ".\n";
    }


    return 0;
}

//output
emplace_back:
I am being constructed.


push_back:
I am being constructed.
I am being moved.


Contents:
Nelson Mandela was elected president of South Africa in 1994.
Franklin Delano Roosevelt was re-elected president of the USA in 1936.

如例子所示,使用emplace_back函数可以减少一次拷贝或移动构造的过程,提升容器插入数据的效率

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容