这段代码中的 VeryHeavy(args...)
被精确复制了多少次?
map.emplace(std::pair<VeryHeavyKey, VeryHeavy>(key, VeryHeavy(args...)));
或者,也许使用 std::make_pair
更好?
复制对象是否有任何标准化保证?将重对象插入 std::map 而不复制的正确方法是什么?
最佳答案
What's the right way to insert heavy object into std::map without copying?
C++17 之前
map.emplace(std::piecewise_construct,
std::forward_as_tuple(std::move(key)),
std::forward_as_tuple(args...));
C++17 后
map.try_emplace(std::move(key), args...);
C++17 变体对前者进行了改进,如果 key
已经存在,则不构造 VeryHeavy
。
关于c++ - 将重物插入 std::map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62751463/