简体   繁体   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.

相关问题 如何在C ++中对字符串进行哈希处理? - How do I hash strings in C++? 如何编写 function 来计算 C++ 中的最近点? - How do I write a function to calculate nearest point in C++? 如何在C ++中使用gcrypt计算大文件的哈希 - How do I calculate the Hash of a large file with gcrypt, in c++ 如何在C或C ++中编写简单的正则表达式模式匹配函数? - How do I write a simple regular expression pattern matching function in C or C++? C ++我是否需要为函数编写throw子句? - C++ Do I need to write throw clause for a function everywhere? 如何在 C++ 中编写此伪代码? 函数返回两个数组到两个初始化数组 - How do I write this pseudocode in C++? Function returns two arrays to two initialized arrays 如何编写引用“char *”或“const char*”的 C++ 函数? - How do I write a C++ function that takes a reference to either a 'char *' or a 'const char*'? 如何编写通用C ++函数将xml解析为结构? - How do I write a generic C++ function to parse xml into structs? C ++:如何编写一个接受迭代器并插入元素的函数? - C++: How do I write a function that accepts an iterator and inserts elements? 如何编写构造函数 function 来设置向量的大小,该向量是 C++ 中的 class 的属性? - How do I write the constructor function that sets the size of a vector which is the attribute of my class in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM