[代码演示][C++] vector中emplace_back和push_back的区别

按照cppreference上的代码,简单测试了下

首先emplace_back和push_back区别在于:

  • push_back会先在新的内存中构造临时对象,再调用拷贝构造函数将该对象拷贝到vector的末尾
  • emplace_back则直接在vector相应的内存上构造对象,不产生临时对象

下面是cppreference的例子:

#include 
#include 
#include 
 
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 main()
{
    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";
    }
}

输出为:

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.

可以看到,push_back调用了两个构造函数


再多添加几个元素:

#include 
#include 
#include 
 
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 main()
{
    std::vector<President> elections;
    std::cout << "emplace_back:\n";
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);
    std::cout << std::endl;
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);
    std::cout << std::endl;
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);
    std::cout << std::endl;

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

输出为:

emplace_back:
I am being constructed.

I am being constructed.
I am being moved.

I am being constructed.
I am being moved.
I am being moved.

Nelson Mandela was re-elected president of South Africa in 1994.
Nelson Mandela was re-elected president of South Africa in 1994.
Nelson Mandela was re-elected president of South Africa in 1994.

为什么也调用了多次拷贝构造呢?因为和原本vector中剩下元素的数量对应,猜测因为发生了整体的vector内存搬移

先进行内存reverse,再进行测试看看

#include 
#include 
#include 
 
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 main()
{
    std::vector<President> elections;
    
    elections.reserve(3);
    
    std::cout << "emplace_back:\n";
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);
    std::cout << std::endl;
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);
    std::cout << std::endl;
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);
    std::cout << std::endl;

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

则可看到输出只调用了普通构造函数

emplace_back:
I am being constructed.

I am being constructed.

I am being constructed.

Nelson Mandela was re-elected president of South Africa in 1994.
Nelson Mandela was re-elected president of South Africa in 1994.
Nelson Mandela was re-elected president of South Africa in 1994.

结论:

  • emplace_back并不能完全代替push_back,失效场景之后待补
  • 尽量使用reserve先请求好够用的空间,对性能的提升很重要
  • 要善用cppreference网站,介绍权威,同时示例代码设计很典型,并且可以在线修改编译测试,很有收获

你可能感兴趣的:([代码演示][C++] vector中emplace_back和push_back的区别)