刚刚用到unordered_map, 而且需要用于自定义的键和值类型, 讨教一下怎么样实现?
原代码中有
typedef std::map< String, PseudoClassPropertyList > PseudoClassPropertyDictionary;
本人想改成
typedef std::unordered_map< String, PseudoClassPropertyList > PseudoClassPropertyDictionary;
请注意, String 不是 std:string
这个自定义类有成员
const char* String::CString()
感谢
原代码中有
typedef std::map< String, PseudoClassPropertyList > PseudoClassPropertyDictionary;
本人想改成
typedef std::unordered_map< String, 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