c++ - C++中的哈希对象问题

标签 c++ function hash hashmap

在某些 C++ 程序中,我有两个类 - AbcXyzAbc 类包含一个整数成员 Foo,如以下 list 所示:

class Abc
{
 public:
 int Foo;
 Abc(int f)
 {
  Foo = f;
 }
};
class Xyz{};

我正在使用 map <Abc, Xyz> 但性能极差。这就是为什么我想使用 hash_map<AbcXyz>(Dev-Cpp 扩展库)和一些棘手的散列。让我们假设以下 AbcHash 一元函数对象将充当哈希生成器:

class AbcHash
{
 public:
 size_t operator() (const Abc &a)
 {
  return a.Foo % 123;
 } 
};

为了与 STL 保持一致,我还实现了一个 AbcCmp 比较器:

class AbcCmp
{
 public:
 bool operator() (const Abc &a1, const Abc &a2)
 {
  return (a1.Foo == a2.Foo);
 }
};

当我最后想使用 hash_map 时,我已经测试了 AbcXyz 类。这是我的主要:

int main(int argc, char *argv[])
{
 hash_map myMap;

 Abc a(10);
 a.Foo = 23;

 Xyz x();

 myMap[a] = x; // this line fails the compilation
}

最后一行导致一些奇怪的编译错误:

Compiler: Default compiler
Building Makefile: "C:\user\hdr\hashfunc\Makefile.win"
Executing  make...
make.exe -f "C:\user\hdr\hashfunc\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   

In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/hash_map.h:59,
                 from main.cpp:2:
C:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the  header for the  header for C++ includes, or  instead of the deprecated header . To disable this warning use -Wno-deprecated.
main.cpp: In function `int main(int, char**)':
main.cpp:45: error: no match for 'operator=' in '(&myMap)->__gnu_cxx::hash_map::operator[] [with _Key = Abc, _Tp = Xyz, _HashFcn = AbcHash, _EqualKey = AbcCmp, _Alloc = std::allocator](((const Abc&)((const Abc*)(&a)))) = x'
main.cpp:14: note: candidates are: Xyz& Xyz::operator=(const Xyz&)

C:/Dev-Cpp/include/c++/3.4.2/ext/hashtable.h: In member function `size_t __gnu_cxx::hashtable::_M_bkt_num_key(const _Key&, size_t) const [with _Val = std::pair, _Key = Abc, _HashFcn = AbcHash, _ExtractKey = std::_Select1st >, _EqualKey = AbcCmp, _Alloc = std::allocator]':
C:/Dev-Cpp/include/c++/3.4.2/ext/hashtable.h:523:   instantiated from `size_t __gnu_cxx::hashtable::_M_bkt_num(const _Val&, size_t) const [with _Val = std::pair, _Key = Abc, _HashFcn = AbcHash, _ExtractKey = std::_Select1st >, _EqualKey = AbcCmp, _Alloc = std::allocator]'
C:/Dev-Cpp/include/c++/3.4.2/ext/hashtable.h:887:   instantiated from `void __gnu_cxx::hashtable::resize(size_t) [with _Val = std::pair, _Key = Abc, _HashFcn = AbcHash, _ExtractKey = std::_Select1st >, _EqualKey = AbcCmp, _Alloc = std::allocator]'
C:/Dev-Cpp/include/c++/3.4.2/ext/hashtable.h:701:   instantiated from `typename __gnu_cxx::hashtable::reference __gnu_cxx::hashtable::find_or_insert(const _Val&) [with _Val = std::pair, _Key = Abc, _HashFcn = AbcHash, _ExtractKey = std::_Select1st >, _EqualKey = AbcCmp, _Alloc = std::allocator]'
C:/Dev-Cpp/include/c++/3.4.2/ext/hash_map:181:   instantiated from `_Tp& __gnu_cxx::hash_map::operator[](const typename __gnu_cxx::hashtable, _Key, _HashFcn, std::_Select1st >, _EqualKey, _Alloc>::key_type&) [with _Key = Abc, _Tp = Xyz, _HashFcn = AbcHash, _EqualKey = AbcCmp, _Alloc = std::allocator]'
main.cpp:45:   instantiated from here
C:/Dev-Cpp/include/c++/3.4.2/ext/hashtable.h:518: error: passing `const AbcHash' as `this' argument of `size_t AbcHash::operator()(const Abc&)' discards qualifiers

make.exe: *** [main.o] Error 1

Execution terminated

我完全不知道这里有什么问题。如何修复?

最佳答案

我的第一个方法是找出 map 性能不佳的原因。 map 实际上在各方面都表现出色——在某些情况下它们可能会胜过哈希表。在不知道问题的情况下更换容器不是一个好主意。

然后我会把 DevC++ 当作垃圾。这个古董有很多问题,不再积极开发——我特别怀疑是否使用它附带的任何库。我会切换到 Code::Blocks 的夜间构建, Twilight Dragon GCC 的构建,以及(如果我仍然想要哈希)到 TR1 或 Boost 哈希表实现。

关于c++ - C++中的哈希对象问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1932959/

相关文章:

python - 使用 totient 函数 - 未定义的问题

MATLAB 错误信息 "This statement is not inside any function."

javascript - 如何在没有事件的情况下调用 ember 辛烷模板内的函数?

javascript - 将 javascript 对象转换为 ruby​​ 哈希值

c++ - 尝试通过 winsock 套接字连接到网站时收到 400 Bad Request

c++ - 如何在 int 和作用域枚举的引用之间进行 static_cast?

c# - 我如何在我的 ASP.Net 应用程序上使用 salting+hashing?

git - 为什么 Git 使用*压缩*对象的 SHA1 而不是原始对象的 SHA1?

c++ - 从指向字符数组的指针中提取输入

c++ - 2个类(class)可以共享一个 friend 功能吗?