C++ 带有自定义散列/相等函数的 unordered_map - 函数不会被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15810620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unordered_map with custom hashing/equal functions - functions don't get called
提问by Paul
This is weird.. the following code (which I managed to compile thanks to Cassio Neri) is compiling without any error.. by the way either hashing_func nor key_equal_func do get called (the couts aren't showing in the console window)
这很奇怪......下面的代码(我设法编译感谢Cassio Neri)正在编译没有任何错误......顺便说一下 hashing_func 和 key_equal_func 都被调用(couts 没有显示在控制台窗口中)
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <functional>
using namespace std;
unsigned long hashing_func(string key)
{
cout << "Hashing called";
unsigned long hash = 0;
for(int i=0; i<key.size(); i++)
{
hash += (71*hash + key[i]) % 5;
}
return hash;
}
template<class T> bool key_equal_fn(T t1, T t2)
{
return t1 == t2;
}
template <> bool key_equal_fn<string>(string t1, string t2)
{
cout << "Equal called";
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> &)(function<bool(string,string)>(key_equal_fn<string>))) ;
bool case_insensitive = mymap.key_eq()("test","TEST");
mymap["paul"] = "jenna";
mymap["frank"] = "ashley";
if(mymap["paul"] == mymap["frank"])
cout << "equal" << endl;
return 0;
}
I'm using MSVC2012, any hint on what could be the problem?
我正在使用 MSVC2012,有什么关于可能是什么问题的提示吗?
采纳答案by Tony The Lion
The problem is that you need to pass the types of your hash function and hash_key_equal function to your unordered_map, and then the actual functions to the ctor of the map.
问题是您需要将哈希函数和 hash_key_equal 函数的类型传递给 unordered_map,然后将实际函数传递给映射的 ctor。
Your unordered_map definition should look like this:
您的 unordered_map 定义应如下所示:
unordered_map<
std::string,
std::string,
std::function<unsigned long(std::string)>,
std::function<bool(std::string, std::string)>
> mymap(n, hashing_func, key_equal_fn<std::string>);
The unordered_map
is a template and it looks like this:
这unordered_map
是一个模板,它看起来像这样:
template<
class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<const Key, T>>
> class unordered_map;
which means if you want to pass new Hash
and KeyEqual
functions you have to tell the template the types of these things.
这意味着如果你想传递 newHash
和KeyEqual
函数,你必须告诉模板这些东西的类型。
Link isn't accessible anymore (Request Update): Live Example
链接不再可用(请求更新): 实时示例
回答by riv
You have to specify hash/compare functions with template arguments, not in the constructor. Here is an example:
您必须使用模板参数指定哈希/比较函数,而不是在构造函数中。下面是一个例子:
class Hasher
{
public:
size_t operator() (string const& key) const
{
cout << "Hashing called";
size_t hash = 0;
for(size_t i=0; i<key.size(); i++)
{
hash += (71*hash + key[i]) % 5;
}
return hash;
}
};
class EqualFn
{
public:
bool operator() (string const& t1, string const& t2) const
{
cout << "Equal called";
return !(t1.compare(t2));
}
};
unordered_map<string, string, Hasher, EqualFn> mymap(5);