C++ 对于HashMap的若干思考

1 hash_map

当使用hash_map时,头文件的引用方式为.
#include <ext/hash_map>
#include "HASH_MAP_HPP.h"
using namespace __gnu_cxx;
以string为key时,会出现以下错误
error: no match for call to ‘(const hasher {aka const __gnu_cxx::hash<std::__cxx11::basic_string<char> >}) (const key_type&)’|

####解决方案,加入以下头文件HASH_MAP_HPP.h即可

#ifndef HASH_MAP_HPP_H_INCLUDED
#define HASH_MAP_HPP_H_INCLUDED
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#include <string>


#if defined(_STLPORT_VERSION)
    #include <hash_map>
    #include <hash_set>
    using std::hash;
    using std::hash_map;
    using std::hash_set;
#else // not using STLPORT


    #ifdef __GNUC__
        #if __GNUC__ >= 3
            #include <ext/hash_map>
            #include <ext/hash_set>
            namespace __gnu_cxx {
                template <>
                struct hash<std::string> {
                    size_t operator()(const std::string& s) const {
                        unsigned long __h = 0;
                        for (unsigned i = 0;i < s.size();++i)
                            __h ^= (( __h << 5) + (__h >> 2) + s[i]);
                        return size_t(__h);

                    }

                };
            }
            using __gnu_cxx::hash_map;
            using __gnu_cxx::hash;
        #else // GCC 2.x
            #include <hash_map>
            #include <hash_set>
            namespace std {
                struct hash<std::string> {
                    size_t operator()(const std::string& s) const {
                        unsigned long __h = 0;
                        for (unsigned i = 0;i < s.size();++i)
                            __h ^= (( __h << 5) + (__h >> 2) + s[i]);
                        return size_t(__h);
                    }
                };
            };
            using std::hash_map;
            using std::hash_set;
            using std::hash;
        #endif // end GCC >= 3
    #elif defined(_MSC_VER) && ((_MSC_VER >= 1300) || defined(__INTEL_COMPILER))
        // we only support MSVC7+ and Intel C++ 8.0
        #include <hash_map>
        #include <hash_set>
        namespace stdext {
            inline size_t hash_value(const std::string& s) {
                unsigned long __h = 0;
                for (unsigned i = 0;i < s.size();++i)
                    __h ^= (( __h << 5) + (__h >> 2) + s[i]);
                return size_t(__h);
            }
        }
        using std::hash_map; // _MSC_EXTENSIONS, though DEPRECATED
        using std::hash_set;
    #else
        #error unknown compiler
    #endif //GCC or MSVC7+
#endif // end STLPORT
#endif /* ifndef HASH_MAP_HPP */

产生原因在SGI STL中,提供了以下hash函数:

struct hash<char*>
struct hash<const char*>
struct hash<char> 
struct hash<unsigned char> 
struct hash<signed char>
struct hash<short>
struct hash<unsigned short> 
struct hash<int> 
struct hash<unsigned int>
struct hash<long> 
struct hash<unsigned long>

也就是说,如果你的key使用的是以上类型中的一种,你都可以使用缺省的hash函数。当然你自己也可以定义自己的hash函数。对于自定义变量,你只能如此,例如对于string,就必须自定义hash函数。

参考网页1
参考网页2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值