【发布时间】:2022-01-07 12:12:56
【问题描述】:
我需要使用std::pair<QColor, char> 作为unordered_map 的键。至于pair,我知道有boost功能可以使用,但是颜色呢?仅在 std 命名空间中提供哈希模板就足够了吗?如果是这样,作为散列基础的颜色的最佳属性是什么,以最大限度地提高性能并最大限度地减少冲突?我的第一个想法是简单的name()。如果是这样
namespace std {
struct hash<Key>
{
std::size_t operator()(const Key& k) const {
return std::hash<std::string>()(k.name());
}
}
以上代码取自C++ unordered_map using a custom class type as the key。
【问题讨论】: