繁体   English   中英

将指针视为unsigned int for hash

[英]Treat a pointer as unsigned int for hash

我想为我的自定义类型AnimationSet重载std :: hash模板:

struct AnimationSet {
    const AnimationData *animationData;
    SceneNode *sceneNode;

    bool operator==(const AnimationSet &other) const {
        return ( this->animationData == other.animationData &&
                 this->sceneNode == other.sceneNode );
    }
};

如您所见,它是一个只包含两个指针的结构。

将这些指针强制转换为unsigned int以计算AnimationSet的哈希值是否合法?

namespace std {
    template<>
    struct hash<AnimationSet> {
        size_t operator()(const AnimationSet &set) const {
            hash<unsigned int> h;

            return h((unsigned int)set.animationData) ^ h((unsigned int)set.sceneNode);
        }
    };
}

编辑:我在哈希重载的上下文中问这个问题,但我想知道更一般的问题的答案:“将任何指针转换为unsigned int是否公平?”

通常,不,指针不一定与unsigned int大小相同。 特别是,在大多数64位系统上,它们将是两倍大,所以你要做的只是取指针的最低32位,这更有可能导致冲突(特别是因为许多指针不会有设置的最低两位,因此您只获得有助于哈希值的30位有用信息。

您应该#include <cstdint>std::uintptr_tstd::uintptr_t 这是一个无符号整数类型,保证能够存储指针的值而不会丢失任何位。

虽然指针实际上只是一个无符号整数值(至少在类似现代PC的系统上),但不确定是否可以使用unsigned int 请记住, int的大小和指针的大小可能不同(例如,在64位系统上)。

如果你想要一个大到足以适合指针的整数类型,请使用<cstdint> uintptr_t

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM