2021-12-01

Qt: how to implement a hash function for QColor?

I have a need to use std::pair<QColor, char> as a key of unordered_map. As for the pair, I know that there is boost functionality that can be used, but what about the color? Is it enough to just provide the hash template in the std namespace? If so, what would be the best attribute of the color to base the hash on to maximize the performance and minimize collisions? My first thought was about simple name(). If so

namespace std {
    struct hash<Key>
    {
        std::size_t operator()(const Key& k) const {
            return std::hash<std::string>()(k.name());
    }
}

The code above is taken from C++ unordered_map using a custom class type as the key.



from Recent Questions - Stack Overflow https://ift.tt/3o7WZQw
https://ift.tt/eA8V8J

No comments:

Post a Comment