簡體   English   中英

如何在C ++中編寫哈希函數?

[英]How do i write a hash function in C++?

如何為自定義類型聲明哈希函數,以便可以在unordered_map使用它?

namespace std {
    template<>
    struct hash<my_custom_type>
    {
        using argument_type = my_custom_type;
        using result_type = size_t;

        size_t operator()(my_custom_type const& x) const
        {
            // Perform your hash algorithm here.
        }
    };
}

可能最好的方法是使用std :: hash <mytype>提供std :: hash <class Key>的特化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM