首页 > 其他 > 详细

unorderd_map 自定义键值及哈希函数的重载

时间:2019-04-16 10:36:01      阅读:406      评论:0      收藏:0      [点我收藏+]
using namespace std;

class PixID{
public:
    int _r;
    int _c;

    PixID(){}

    PixID(int r, int c){
        _r = r;
        _c = c;
    }
    // bool operator==(const PixID & p) const 
    // {
    //    return _r == p._r && _c == p._c;
    // }
};

// inline size_t PixID_hash( const PixID & p ) 
// {
//     return std::hash<int>()(p._r) ^ std::hash<int>()(p._c);
// }

namespace std{
    template<>
    struct hash<PixID>{//哈希的模板定制
    public:
        size_t operator()(const PixID &p) const 
        {
            return hash<int>()(p._r) ^ hash<int>()(p._c);
        }
        
    };
    
    template<>
    struct equal_to<PixID>{//等比的模板定制
    public:
        bool operator()(const PixID &p1, const PixID &p2) const
        {
            return p1._r == p2._r && p1._c == p2._c;
        }
        
    };
}

参考链接: https://blog.csdn.net/y109y/article/details/82669620

unorderd_map 自定义键值及哈希函数的重载

原文:https://www.cnblogs.com/walnuttree/p/10714872.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!