C++11 引入了 vector::emplace_back() ,此前 vector 只能调用 push_back 来添加元素。一个简单的示例来对比两者区别:
#include <iostream>
#include <vector>
using namespace std;
struct A {
A() { cout << "Default constructor: " << val << endl; }
A(int v) : val(v) { cout << "Construct with val: " << val << endl; }
A(const A& a) : val(a.val + 10) { cout << "Copy constructed: " << val << endl; }
A(A&& a) : val(a.val + 100) { cout << "Move constructed: " << val << endl; }
~A() { cout << "Destructed: " << val << endl; }
int val { 0 };
};
int main()
{
A a(1), b(2);
vector<A> vec;
vec.reserve(4);
cout << endl;
cout << "push_back existing item:\n";
vec.push_back(a);
cout << endl;
cout << "emplace_back existing item:\n";
vec.emplace_back(b);
cout << endl;
cout << "push_back non-existing item:\n";
vec.push_back(3);
cout << endl;
cout << "emplace_back non-existing item:\n";
vec.emplace_back(4);
cout << endl;
}
执行结果:
Construct with val: 1
Construct with val: 2
push_back existing item:
Copy constructed: 11
emplace_back existing item:
Copy constructed: 12
push_back non-existing item:
Construct with val: 3
Move constructed: 103
Destructed: 3
emplace_back non-existing item:
Construct with val: 4
Destructed: 11
Destructed: 12
Destructed: 103
Destructed: 4
Destructed: 2
Destructed: 1
可以看到,当被添加的元素已经存在时,push_back 与 emplace_back 是一样的。当元素事先不存在时,emplace_back 是直接在原地构造一个对象,而 push_back 会先构造一个临时对象,再将其移动到 vector 中,然后析构掉临时对象。基于这样的表现,我们在平时大部分情况下都应当用 emplace_back 替代 push_back 。
那是不是 push_back 就一无是处了呢?
Why would I ever use push_back instead of emplace_back? 中提到,当使用隐式构造函数时,push_back 能够提供更安全的编译检查,比如:
vector<unique_ptr<int>> v;
int a;
v.emplace_back(std::addressof(a));
v.push_back(std::addressof(a));
vector<int8_t> vec;
int a = 1000;
vec.emplace_back(a);
vec.push_back(a);
|