C++ std::vector emplace vs insert(C++ std::vector emplace 与插入)
本文介绍了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 与插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:C++ std::vector emplace 与插入


猜你喜欢
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- C++ 协变模板 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 近似搜索的工作原理 2021-01-01