我试图定义一种具有自定义散列函数和相等比较函数的unordered_map.这些函数的函数原型如下:
//set<Vertex3DXT*> is the type of the key; Cell3DXT* is the type of the value
size_t VertexSetHashFunction(set<Vertex3DXT*> vertexSet); //hash function
bool SetEqual(set<Vertex3DXT*> a, set<Vertex3DXT*> b); //equality
Run Code Online (Sandbox Code Playgroud)
我声明了这些函数原型,然后我尝试声明类型如下:
typedef std::tr1::unordered_map<set<Vertex3DXT*>, Cell3DXT*, VertexSetHashFunction, SetEqual> CellDatabaseMapType;
Run Code Online (Sandbox Code Playgroud)
但它表示VertexSetHashFunction和SetEqual不是有效的模板类型参数.文档很混乱,因为它没有准确说明模板参数应该是什么类型 - 我只是应该给它像我在这里做的那样,或者是否有一些其他类型的对象封装了函数(因为文档确实谈到"哈希函数对象类型")?
编辑后回答:
<应该提供std::map.有关最佳实践的更多信息,请访问James McNellis的答案.
这个问题中包含的代码编写得很糟糕.这只是因为我正在玩SPOJ并且输入数据严格有效.这种std::string方法是我最初选择的方法,但事实证明还不够快.
谢谢.
我知道我不能char[]直接用地图,比如map<char[], int>.因此我把它放在课堂上.但它仍然可以通过编译.怎么处理?
#include <stdio.h>
#include <map>
using namespace std;
class id {
public:
char v [30];
};
int main () {
map<id, int> m;
id a;
while (gets(a.v)) {
m[a]++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = id]’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_map.h:418: instantiated from ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = id, _Tp …Run Code Online (Sandbox Code Playgroud)