我需要C++(STL)中的hash_map类.主要操作是将对放入集合中,然后检查它是否存在.
我无法找到一个示例代码来确定我是否正确声明了.
#include <iostream>
#include <hash_map>
using namespace std;
using namespace __gnu_cxx;
typedef pair<int,string> pis;
struct eqpis {
bool operator()(pis p1,pis p2) const {
if(p1==p2) return true;
return false;
}
};
int main() {
hash_map<pis,int,hash<pis>,eqpis> map;
}
Run Code Online (Sandbox Code Playgroud)
这个编译.但是如果我添加这行:map [pis(10,"hello")] = 10; 然后它会产生很多错误:
/usr/include/c++/4.4/backward/hashtable.h:在成员函数'size_t __gnu_cxx :: hashtable :: _ M_bkt_num_key(const _Key&,size_t)const [with _Val = std :: pair,std :: allocator >>, int>,_ Key = std :: pair,std :: allocator >>,_ HashFcn = __gnu_cxx :: hash,std :: allocator >>>,_ ExtractKey = std :: _ Select1st,std :: allocator >>,int >>, _EqualKey = eqpis,_Alloc = std :: allocator]':/ usr /include/c++/4.4/backward/hashtable.h:594:从'size_t __gnu_cxx :: hashtable :: _ M_bkt_num(const _Val&,size_t)const [实例化] _Val = std :: pair,std :: allocator >>,int>,_ Key = std :: pair,std :: allocator >>,_ HashFcn = __gnu_cxx :: hash,std :: allocator >>> ,, _ ExtractKey = std: :_Select1st,std :: allocator >>,int >>,_ EqualKey = eqpis,_Alloc = std :: allocator]'/usr/include/c++/4.4/backward/hashtable.h:1001:从'void __gnu_cxx ::实例化:: hashtable :: resize(size_t)[与_Val = std :: pair,std :: allocator >>,int>,_ Key = std :: pair,std :: allocator >>,_ HashFcn = __gnu_cxx :: hash,std ::分配器>>>,_ ExtractKey = std :: _ Select1st,std :: allocator >>,int >>,_ EqualKey = eqpis,_Alloc = std :: allocator]'/ usr /include/c++/4.4/backward/hashtable.h:789:从'实例化' _Val&__gnu_cxx :: hashtable :: find_or_insert(const _Val&)[with _Val = std :: pair,std :: allocator >>,int>,_ Key = std :: pair,std :: allocator >>,_ HashFcn = __gnu_cxx :: hash,std :: allocator >>>,_ ExtractKey = std :: _ Select1st,std :: allocator >>,int >>,_ EqualKey = eqpis,_Alloc = std :: allocator]'/usr/include/c++/4.4/backward/hash_map:216:从'_Tp&__gnu_cxx :: hash_map :: operator []实例化(const typename __gnu_cxx :: hashtable,_Key,_HashFn,std :: _ Select1st>,_ EqualKey,_Alloc> :: key_type&)[with _Key = std: :pair,std :: allocator >>,_ Tp = int,_HashFn = __gnu_cxx :: hash,std :: allocator >>>,_EqualKey = eqpis,_Alloc = std :: allocator]'x.cpp:18:从这里实例化/usr/include/c++/4.4/backward/hashtable.h:590:错误:无法调用'(const __gnu_cxx :: hash,std :: allocator> >>)(const std :: pair,std ::异体 cator >> &&)'
谢谢
首先,你想要std::unordered_map或unordered_set.您的类需要operator=(或者您需要一个EqualityCompare类),并且您需要一个哈希类operator(),它将您的键类型作为参数并返回一个size_t.
| 归档时间: |
|
| 查看次数: |
10128 次 |
| 最近记录: |