简体   繁体   English

试图将函子放置在std :: unordered_map中

[英]Trying to emplace functor in std::unordered_map

I got my inventory 我有库存

std::unordered_map<std::reference_wrapper<Item>, int, ItemHasher> items;

ItemHasher is ItemHasher

struct ItemHasher
{
    std::size_t operator()(const std::reference_wrapper<Item>& item) const
    {
        return item.get().getHash();
    }
};

How to emplace item in my map? 如何在我的地图上放置物品? I tried 我试过了

items.try_emplace(item, 1, /*Different variants*/);

But it isn't working as it should. 但是它没有按预期的方式工作。

unordered_map stores the hasher it uses already. unordered_map存储已使用的哈希器。 It makes scant sense to pass another one to try_emplace . 将另一个传递给try_emplace

items.try_emplace(item, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM