C++ std::vector emplace 与插入 [英] C++ std::vector emplace vs insert

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

问题描述

我想知道两者之间有什么区别.我注意到 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 接受构建对象所需的参数,而 insert 接受(引用)一个对象.

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 与插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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