emplace_back 和push_back 都是向容器内添加数据.
对于在容器中添加类的对象时, 相比于push_back ,emplace_back 可以避免额外类的复制和移动 操作.
"emplace_back avoids the extra copy or move operation required when using push_back."
为了证实上述论断,我们自定义一个类,并在普通构造函数、拷贝构造函数、移动构造函数中打印相应描述:
#include <vector>
#include <string>
#include "time_interval.h"
class Foo {
public :
Foo(std ::string str) : name(str) {
std ::cout << "constructor" << std ::endl;
}
Foo(const Foo& f) : name(f.name) {
std ::cout << "copy constructor" << std ::endl;
}
Foo(Foo&& f) : name(std ::move(f.name)){
std ::cout << "move constructor" << std ::endl;
}
private :
std ::string name;
};
int main() {
std ::vector <Foo> v;
int count = 10000000 ;
v.reserve(count);
{
Foo temp("ceshi" );
v.push_back(temp);
}
v.clear();
{
Foo temp("ceshi" );
v.push_back(std ::move(temp));
}
v.clear();
{
v.push_back(Foo("ceshi" ));
}
v.clear();
{
std ::string temp = "ceshi" ;
TIME_INTERVAL_SCOPE("push_back(string):" );
v.push_back(temp);
}
v.clear();
{
std ::string temp = "ceshi" ;
v.emplace_back(temp);
}
}