如何在c++标准库中启用哈希

How can I enable hash in C++ Standard Library?

本文关键字:启用 哈希 标准 c++      更新时间:2023-10-16

我试过使用#include<hash_map>#include <hash_set>,我仍然得到相同的错误。

下面是我的代码:
void HashTable_chaining::remove( const string & x )
{
    int hash_index = hash( x, theLists.size( ) ) ;
    list<string>&  whichList = theLists[ hash_index ];
    // search to make sure element not present
    for(list<string>::iterator itr=whichList.begin();itr!=whichList.end();itr++) {
        if(*itr==x) {
            theLists[hash_index].erase(itr);
            return;
        }
    }
    // element not found - so nothing to remove
}

我的错误是:

Error   8   error C2872: 'hash' : ambiguous symbol  c:usersaaron           johnsondesktopprogram 5(johnson- noakes)program 5(johnson- noakes)chaining.cpp 32  1   Program 5(Johnson- Noakes)

有8个这样的错误。有什么建议吗?我怎样才能找出哪些头必须包括使用哈希?

是否是您自己的函数?如果是这样,请尝试将其放在您自己的名称空间中,然后调用像

这样的函数
int hash_index = yournamespace::hash( x, theLists.size() );

如果你想使用std::hash:它定义在

#include <functional>

你可以在这里找到完整的c++规范,包括我们的朋友"std::hash":

http://en.cppreference.com/w/cpp/utility/hash

hash_set和hash_map在SGI STL中可用。请看下面的页面:

  • http://www.sgi.com/tech/stl/hash_map.html
  • http://www.sgi.com/tech/stl/hash_set.html

文档和源代码可在此链接获得:

  1. http://www.sgi.com/tech/stl/download.html