【发布时间】:2010-12-21 12:30:32
【问题描述】:
我需要 C++(STL) 中的 hash_map 类。主要操作是将pair放入集合中,然后检查它是否存在。
我无法找到一个示例代码来判断我的声明是否正确。
#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;
}
这个编译。但是,如果我添加以下行: 地图[pis(10,"你好")]=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 [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:1001: 实例化自 'void __gnu_cxx::hashtable::resize(size_t) [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: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::分配器 > >&)'
谢谢
【问题讨论】:
-
为什么
if(p1==p2) return true; ...冗长?