我需要像这样定义一个unordered_map unordered_map<pair<int, int>, *Foo>
,定义和传递一个hash
和equal
函数到这个地图的语法是什么?
我试过传递给它这个对象:
class pairHash{
public:
long operator()(const pair<int, int> &k) const{
return k.first * 100 + k.second;
}
};
Run Code Online (Sandbox Code Playgroud)
没有运气:
unordered_map<pair<int, int>, int> map = unordered_map<pair<int, int>, int>(1,
*(new pairHash()));
Run Code Online (Sandbox Code Playgroud)
我不知道是什么size_type_Buskets
意思所以我给了它1
.做正确的方法是什么?谢谢.
我以为我可以指向一个完全专用的模板函数,但下面的代码没有编译(MSVC2012)
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
unsigned long hashing_func(string key)
{
unsigned long hash = 0;
for(int i=0; i<key.size(); i++)
{
hash += (71*hash + key[i]) % 5;
}
return hash;
}
bool key_equal_fn2(string t1, string t2)
{
return t1 == t2;
}
template<class T> bool key_equal_fn(T t1, T t2)
{
return t1 == t2;
}
template <> bool key_equal_fn<string>(string t1, string t2)
{
return !(t1.compare(t2));
}
int main ()
{
unordered_map<string, string>::size_type n …
Run Code Online (Sandbox Code Playgroud)