在C ++中向量插入和emplace之间的区别是什么 [英] What is difference between insert and emplace for vector in C++

查看:126
本文介绍了在C ++中向量插入和emplace之间的区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了单向插入使用emplace和多重插入使用向量中插入,
在它们的实现中有任何其他差异吗?

Apart from single insertion using emplace and multiple insertion using insert in vector, is there any other difference in their implementation?

任何元素都会移动所有其他元素。

As in both cases inserting any element will shift all other elements.

推荐答案

std :: vector :: insert 复制移动元素。通过调用复制构造函数或移动构造函数,

while,

std :: vector :: emplace 元素中构建,即不执行复制或移动操作。

std::vector::insert copies or moves the elements in to the container by calling copy constructor or move constructor.
while,
In std::vector::emplace elements are constructed in-place, i.e. no copy or move operations are performed.

后者是从C ++ 11开始引入的,如果你的类的复制是一个非平凡的操作,它的用法是可取的。

The later was introduced since C++11 and is its usage is desirable if copying for your class is an non trivial operation.

这篇关于在C ++中向量插入和emplace之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆