看看这个有趣的演讲:
CppCon 2017: Matt Kulukundis “Designing a Fast, Efficient, Cache-friendly Hash Table, Step by Step”
他在 38:32 分钟左右提到
void Benchmark_Slow(int iters) {
std::unordered_map<string, int> m;
std::pair<const string, int> p = {};
while (iters--) m.insert(p)
}
比下面的变体慢 2 倍
void Benchmark_Fast(int iters) {
std::unordered_map<string, int> m;
const std::pair<const string, int> p = {};
while (iters--) m.insert(p)
}
我还在想为什么 &&
将选择重载 (1)。
std::pair<iterator,bool> insert( value_type&& value ); (1)
std::pair<iterator,bool> insert( const value_type& value ); (3)
哪里value_type
是std::pair<const Key, T>
.
毕竟,我们没有移动值,所以在我的理解中,表达式 p
应该是左值而不是 x/prvalue,对吗?谁能赐教一下?
最佳答案
你不接受有问题的重载:
std::pair<iterator,bool> insert(const value_type& value); // (1)
template< class P >
std::pair<iterator,bool> insert(P&& value); // (2)
P
推导为 value_type&
。
关于c++ - std::map 插入 && 重载导致复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50398597/