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 不复制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
断言失败(size.width>0 && size.height&gAssertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(断言失败(size.width0 amp;amp; size.height0))
在 C++ 中旋转图像而不使用 OpenCV 函数Rotate an image in C++ without using OpenCV functions(在 C++ 中旋转图像而不使用 OpenCV 函数)
OpenCV:处理每一帧OpenCV: process every frame(OpenCV:处理每一帧)
为什么我不能在 openCV 中打开 avi 视频?Why can#39;t I open avi video in openCV?(为什么我不能在 openCV 中打开 avi 视频?)
OpenCV 无法设置 SVM 参数OpenCV unable to set up SVM Parameters(OpenCV 无法设置 SVM 参数)
使用 cvtColor 转换单一颜色Convert a single color with cvtColor(使用 cvtColor 转换单一颜色)