[C++] push_back vs emplace_back

push_back vs emplace_back

总结:

push_back分为两步:先创建一个临时的构造器,然后将这个临时构造器移动或者拷贝到目标容器中。

emplace_back仅有一步:直接在目标容器的目标位置,原地创建构造器即可,无需移动或者拷贝操作。

关键观点:函数void emplace_back(Type&& _Val)完全等价于push_back(Type&& _Val),没有意义,因为是多余的。函数void emplace_back(Args&&...)特别有用,它无需采用变量类型value_type,而是采用可变参数列表,因此,这意味着可以完美地转发参数并直接将对象构造到容器中,而无需任何临时操作。

原文如下:

高赞回复1

In addition to what visitor said :

The function void emplace_back(Type&& _Val) provided by MSCV10 is non conforming and redundant, because as you noted it is strictly equivalent to push_back(Type&& _Val).

But the real C++0x form of emplace_back is really useful: void emplace_back(Args&&...);

Instead of taking a value_type it takes a variadic list of arguments, so that means that you can now perfectly forward the arguments and construct directly an object into a container without a temporary at all.

That’s useful because no matter how much cleverness RVO and move semantic bring to the table there is still complicated cases where a push_back is likely to make unnecessary copies (or move). For example, with the traditional insert() function of a std::map, you have to create a temporary, which will then be copied into a std::pair<Key, Value>, which will then be copied into the map :

std::map<int, Complicated> m;
int anInt = 4;
double aDouble = 5.0;
std::string aString = "C++";

// cross your finger so that the optimizer is really good(形参为value_type)
m.insert(std::make_pair(4, Complicated(anInt, aDouble, aString))); 

// should be easier for the optimizer (形参为可变参数列表)
m.emplace(4, anInt, aDouble, aString);

高赞回复2

// constructs the elements in place.                                                
emplace_back("element");

//It will create new object and then copy(or move) its value of arguments.
push_back(explicitDataType{"element"});

高赞回复3

Specific use case for emplace_back: If you need to create a temporary object which will then be pushed into a container, use emplace_back instead of push_back. It will create the object in-place within the container.

Notes:

  1. push_back in the above case will create a temporary object and move it into the container. However, in-place construction used for emplace_back would be more performant than constructing and then moving the object (which generally involves some copying).
  2. In general, you can use emplace_back instead of push_back in all the cases without much issue. (See exceptions)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值