unordered_map构造函数错误(等于模板函数) [英] unordered_map constructor error (equal_to templated function)
问题描述
我想我可以有一个指向一个完全专门的模板函数的指针,但下面的代码不编译(MSVC2012)
I thought I could have a pointer to a fully-specialized template function, but the following code isn't compiling (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 = 5;
unordered_map<string, string> mymap(n, (const std::hash<string> &)hashing_func, (const std::equal_to<string> &)(key_equal_fn<string>)) ;
mymap["paul"] = "jenna";
mymap["frank"] = "ashley";
return 0;
}
构造函数行返回以下错误:
The constructor line is returning the following error:
错误C2440:'type cast':无法从'bool(__cdecl *)(T,T)'
转换为'const std :: equal_to& _Ty>&'
error C2440: 'type cast' : cannot convert from 'bool (__cdecl *)(T,T)' to 'const std::equal_to<_Ty> &'
推荐答案
两者 hashing_func
和 key_equal_fn
应该是函子对象(而不是函数)。此外,它们的类型必须提供给 unordered_map
模板,即地图应该具有以下类型:
Both hashing_func
and key_equal_fn
should be functor objects (and not functions). In addition, their types must be provided to the unordered_map
template, that is, the map should have this type:
unordered_map<string, string, hashing_func, key_equal_fn>
其中 hashing_func
和 key_equal_fn
是函数类:
struct hashing_func {
unsigned long operator()(const string& key) const {
unsigned long hash = 0;
for(size_t i=0; i<key.size(); i++)
hash += (71*hash + key[i]) % 5;
return hash;
}
};
struct key_equal_fn {
bool operator()(const string& t1, const string& t2) const {
return !(t1.compare(t2));
}
};
然后,以这种方式定义 mymap
:
Then, mymap
is defined in this way:
typedef unordered_map<string, string, hashing_func, key_equal_fn> MapType;
MapType::size_type n = 5;
MapType mymap(n, hashing_func(), key_equal_fn());
或者, hashing_func
和/ c $ c> key_equal_fn 可以是函数,但是你必须将它们包装到 std :: function
对象中。也就是说,
Alternatively, hashing_func
and/or key_equal_fn
can be functions but you have to wrapp them into std::function
objects. That is,
unsigned long hashing_func(const string& key) {
unsigned long hash = 0;
for(size_t i=0; i<key.size(); i++)
hash += (71*hash + key[i]) % 5;
return hash;
}
bool key_equal_fn(const string& t1, const string& t2){
return !(t1.compare(t2));
}
并定义 mymap
typedef unordered_map<string, string,
std::function<unsigned long(const string&)>,
std::function<bool(const string&, const string&)>> MapType;
MapType::size_type n = 5;
MapType mymap(n, hashing_func, key_equal_fn);
如果你愿意,你可以使用lambdas并禁止写两个函数或函子类: p>
If you wish, you can use lambdas and refrain from writing the two funcions or functor classes:
typedef unordered_map<string, string,
std::function<unsigned long(const string&)>,
std::function<bool(const string&, const string&)>> MapType;
MapType mymap(n,
[](const string& key) -> unsigned long {
unsigned long hash = 0;
for(size_t i=0; i<key.size(); i++)
hash += (71*hash + key[i]) % 5;
return hash;
},
[](const string& t1, const string& t2) {
return !(t1.compare(t2));
});
最后,我最喜欢的是 all-lambdas
Finally, my favorite is an all-lambdas solution
auto hashing_func = [](const string& key) -> unsigned long {
unsigned long hash = 0;
for(size_t i=0; i<key.size(); i++)
hash += (71*hash + key[i]) % 5;
return hash;
};
auto key_equal_fn = [](const string& t1, const string& t2) {
return !(t1.compare(t2));
};
typedef unordered_map<string, string,
decltype(hashing_func), decltype(key_equal_fn)> MapType;
MapType::size_type n = 5;
MapType mymap(n, hashing_func, key_equal_fn);
这篇关于unordered_map构造函数错误(等于模板函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!