C++ unordered_map using a custom class type as the key

前端 未结 4 1046
梦如初夏
梦如初夏 2020-11-21 23:32

I am trying to use a custom class as key for an unordered_map, like the following:

#include 
#include 
#include         


        
4条回答
  •  北海茫月
    2020-11-22 00:30

    For enum type, I think this is a suitable way, and the difference between class is how to calculate hash value.

    template 
    struct EnumTypeHash {
      std::size_t operator()(const T& type) const {
        return static_cast(type);
      }
    };
    
    enum MyEnum {};
    class MyValue {};
    
    std::unordered_map> map_;
    

提交回复
热议问题