How to properly hash the custom struct?

前端 未结 2 1745
轮回少年
轮回少年 2020-12-17 17:50

In the C++ language there is the default hash-function template std::hash for the most simple types, like std::string, int, e

2条回答
  •  误落风尘
    2020-12-17 18:31

    boost::hash_combine is something that could help you out here:

    namespace std
    {
    template <>
    struct hash
    {
        std::size_t operator()(const CustomType& c) const
        {
            std::size_t result = 0;
            boost::hash_combine(result, field1);
            boost::hash_combine(result, field2);
            return result;
        }
    };
    }
    

    See the boost doc here.

提交回复
热议问题