C ++ std :: vector emplace vs insert [英] C++ std::vector emplace vs insert

查看:297
本文介绍了C ++ std :: vector emplace vs insert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道两者之间有什么区别。我注意到emplace是c ++ 11添加。那么为什么添加?

I was wondering what are the differences between the two. I notice that emplace is c++11 addition. So why the addition ?

推荐答案

Emplace接受必要的参数来构造一个对象,一个对象。

Emplace takes the arguments necessary to construct an object in place, whereas insert takes (a reference to) an object.

struct Foo
{
  Foo(int n, double x);
};

std::vector<Foo> v;
v.emplace(someIterator, 42, 3.1416);
v.insert(someIterator, Foo(42, 3.1416));

这篇关于C ++ std :: vector emplace vs insert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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