std::hash 专业化仍未被 std::unordered_map 使用

std::hash specialization remains unused by std::unordered_map

我正在尝试通过为 const char 提供专业化来扩展 std::hash<T>,以便我可以使用 const char* 作为 std::unordered_map 中的键类型。

这是我试过的:

#include <unordered_map>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

namespace std
{
    template<>
    struct hash<const char*>
    {
        size_t operator()(const char* const& s) const
        {
           size_t h = 0;
           const char* tmp = s;

           while (*tmp)
              h = (h << 5) + h + (unsigned char)toupper(*tmp++);

           printf("hash of '%s' is (%ld)\n", s, h);
           return h;
        }
    };
}

int main(int argc, char* argv[])
{
   const char* name1= "Mark Nelson";
   const char* name2= strdup(name1);

   std::unordered_map<const char*, int> map;

   printf("Insert (%s)\n", name1);
   map[name1]= 100;

   printf("Lookup (%s)\n", name1);
   printf("map[%s](name1) = %d\n", name1, map.find(name1) != map.end() ? map.find(name1)->second : -1);
   printf("Lookup (%s)\n", name2);
   printf("map[%s](name2) = %d\n", name2, map.find(name2) != map.end() ? map.find(name2)->second : -1);

   return 0;
}

输出是什么:

Insert (Mark Nelson)
hash of 'Mark Nelson' is (121066894705597050)
Lookup (Mark Nelson)
hash of 'Mark Nelson' is (121066894705597050)
hash of 'Mark Nelson' is (121066894705597050)
map[Mark Nelson](name1) = 100
Lookup (Mark Nelson)
hash of 'Mark Nelson' is (121066894705597050)
map[Mark Nelson](name2) = -1

所以在我看来,std::unordered_map 正在使用我提供的哈希实现来进行插入和查找。但由于某种原因,它没有通过 name2 找到值,这似乎是 still 使用指针比较。

这里有什么问题?我使用的是 GCC 4.8.2,文件是用 g++ -std=c++11 main.cc

编译的

无序地图 (ref) 需要两件事:

  1. 哈希函数
  2. 相等比较函数

所以你有 (1) 这似乎有效,但随后地图必须检查 const char * 是否相等,这会转化为指针比较。为此,您可以指定自定义比较器 (ref):

struct cmp_str
{
   bool operator()(char const *a, char const *b)
   {
      return std::strcmp(a, b) == 0;
   }
};

std::unordered_map<const char*, int, std::hash<const char *>, cmp_str> BlahBlah;