std::map emplace without copying value(std::map emplace 不复制值)
问题描述
C++11 std::map<K,V>
类型有一个 emplace
函数,许多其他容器也是如此.
The C++11 std::map<K,V>
type has an emplace
function, as do many other containers.
std::map<int,std::string> m;
std::string val {"hello"};
m.emplace(1, val);
此代码如宣传的那样工作,直接替换 std::pair
,但它会生成 key
和 val<的副本/code> 正在发生.
This code works as advertised, emplacing the std::pair<K,V>
directly, however it results in a copy of key
and val
taking place.
是否也可以将值类型直接嵌入到地图中?我们能比将调用中的参数移动到 emplace
做得更好吗?
Is it possible to emplace the value type directly into the map as well? Can we do better than moving the arguments in the call to emplace
?
这是一个更全面的例子:
Here's a more thorough example:
struct Foo
{
Foo(double d, string s) {}
Foo(const Foo&) = delete;
Foo(Foo&&) = delete;
}
map<int,Foo> m;
m.emplace(1, 2.3, string("hello")); // invalid
推荐答案
你传递给 map::emplace
的参数会被转发给 map::value_type
的构造函数, 即 pair
.所以你可以使用 std::pair
的 分段构造构造函数避免中间复制和移动.
The arguments you pass to map::emplace
get forwarded to the constructor of map::value_type
, which is pair<const Key, Value>
. So you can use the piecewise construction constructor of std::pair
to avoid intermediate copies and moves.
std::map<int, Foo> m;
m.emplace(std::piecewise_construct,
std::forward_as_tuple(1),
std::forward_as_tuple(2.3, "hello"));
现场演示
这篇关于std::map emplace 不复制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::map emplace 不复制值


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30