随笔 - 3  文章 - 2  trackbacks - 0
<2012年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

stdext::hash_map使用字符串(const char*)做key的话,不是只指定一个compare函数难么简单,要给定一个结构体,其包括hash函数,compare函数,以及“桶设定”

struct StringCompare 
{
//define hash function for strings 
    enum 
    { 
        //parameters for hash table 
        bucket_size = 4,  // 一个桶4byte长度(因为sizeof(char*)=4)
        min_buckets = 8 // 最少存在8个桶
    };
    size_t operator()(const char* str) const 
    { 
        unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
    }
    bool operator()(const char *s1, const char* s2) const 
    {    
        if (strcmp(s1, s2) == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    } 
}; 

然后

stdext::hash_map<const char*, Your Data Type, StringCompare>

posted on 2012-08-14 20:40 寰子 阅读(402) 评论(0)  编辑 收藏 引用 所属分类: The C++ programming language

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理