求写一下std::unordered_map的自定义模板

C++语言 码拜 8年前 (2016-06-09) 950次浏览
刚刚用到unordered_map, 而且需要用于自定义的键和值类型, 讨教一下怎么样实现?
原代码中有
typedef std::mapString, PseudoClassPropertyList > PseudoClassPropertyDictionary;
本人想改成
typedef std::unordered_mapString, PseudoClassPropertyList > PseudoClassPropertyDictionary;
请注意,   String 不是 std:string
这个自定义类有成员
const char* String::CString()
感谢
解决方案

30

提供两个东西,一个是计算String对象hash值的仿函数以及一个比较String相等的仿函数

struct str_hash_obj{
    uint32_t operator()(const String& str) const {
       ...
    }
};
struct compare_str {
    bool operator()(const String& str1,const String& str2) const{
       ...
    }
};

40

    template<> struct hash<String>
    {
        typedef String argument_type;
        typedef std::size_t result_type;
        result_type operator()(argument_type const& s) const
        {
			本人的hash算法..
        }
    };

10

String换成string,可以直接使用。

40


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求写一下std::unordered_map的自定义模板
喜欢 (0)
[1034331897@qq.com]
分享 (0)