Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i write a hash function in C++?

Tags:

c++

c++11

hash

How do i declare a hash function for my custom type so that i could use it in an unordered_map?

like image 656
kinokijuf Avatar asked Feb 22 '26 23:02

kinokijuf


1 Answers

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.
        }
    };
}
like image 167
Simple Avatar answered Feb 24 '26 12:02

Simple