std::map::insert
R 值有过载:
std::pair<iterator,bool> insert(value_type&&);
任何绑定到此重载的表达式都将调用 R 值构造函数。自从std::map<K,V>::value_type
is std::pair<const key_type, mapped_type>
, and std::pair
有一个采用 R 值的构造函数:
template<class U1, class U2>
pair(U1&& x, U2&& y);
那么你可以保证 R 值构造函数key_type
and mapped_type
将在创建时被调用pair
对象,并且在映射插入中,只要使用创建 R 值的表达式插入该对,例如:
map1.insert(std::make_pair(0, Huge());
OR
map1.insert(std::make_pair(0, std::move(huge1));
当然,这一切都取决于Huge
具有适当的 R 值构造函数:
Huge(Huge&& h)
{
...
}
最后,您还可以使用std::map::emplace
如果你只是想构建一个新的Huge
对象作为地图中的元素。