C++ 如何使自定义类型作为无序映射中的键工作
我正在尝试让自定义哈希表处理自定义类型。 指 我有:C++ 如何使自定义类型作为无序映射中的键工作,c++,stl,C++,Stl,我正在尝试让自定义哈希表处理自定义类型。 指 我有: typedef pair<int, int> tCoord; struct hashing_func { unsigned long operator()(const tCoord& key) const { unsigned long hash = 0; int h1 = key.first; int h2 = key.second; return
typedef pair<int, int> tCoord;
struct hashing_func {
unsigned long operator()(const tCoord& key) const {
unsigned long hash = 0;
int h1 = key.first;
int h2 = key.second;
return h1 ^ (h2 << 1);
}
};
struct key_equal_fn {
bool operator()(const tCoord& t1, const tCoord& t2) const {
return t1.first == t2.first && t1.second == t2.second;
}
};
unordered_map<tCoord, int, hashing_func, key_equal_fn> coord2cnt;
unordered_map<tCoord, int, hashing_func, key_equal_fn>::iterator iter;
iter = coord2cnt.find(coord);
还尝试将其用作coord2cnt[coord]
,但也在缺少[]
运算符时出错
我正在使用g++4.2进行编译,这有点旧,但在编译以下内容(从上面的链接)时做得很好:
typedef无序映射类型;
MapType::大小\类型n=5;
MapType mymap(n,hashing_func(),key_equal_fn());
还想知道为什么这种类型的定义会起作用,即在第一个参数中指定5。无序的地图API中似乎缺少这种定义方式
有人知道这里出了什么问题吗?
谢谢
更新:正如前面指出的,string是一个内部类,它有一个内置的哈希函数。因此,我在这里重新表述了这个问题,使用自定义类型 您自定义的函数“key_equal_fn”类型是[string,string], 与“text2cnt”不匹配 如果您希望以完全专用的模板方式执行此操作,请执行以下操作:
template <typename T>
struct key_equal_fn {
bool operator()(const T& t1, const T& t2) const {
return t1==t2;
}
};
template<>
struct key_equal_fn<string> {
bool operator()(const string& t1, const string& t2) const {
return !(t1.compare(t2));
}
};
unordered_map<string, string, hashing_func, key_equal_fn<string> > text2cnt;
unordered_map<string, string, hashing_func, key_equal_fn<string> >::iterator iter;
模板
结构键等于{
布尔运算符()(常数T&t1,常数T&t2)常数{
返回t1==t2;
}
};
模板
结构键等于{
布尔运算符()(常量字符串和t1、常量字符串和t2)常量{
返回!(t1.比较(t2));
}
};
无序地图文本2CNT;
无序映射::迭代器iter;
字符串作为键的无序映射在内部处理。您不必将字符串的哈希函数定义为键
如果要将类或结构作为键传递。然后,您必须为它定义一个哈希函数,以计算冲突最小的键的哈希索引。要使用.find查找结构或类对象,请在正在使用的结构或类中重载bool运算符==
例如:
#include <iostream>
#include<unordered_map>
int main() {
std::unordered_map<std::string, int> myMap;
myMap.insert({"asd", 1});
myMap.insert({"qwe", 2});
std::string input;
std::cout<<"Enter String: ";
std::cin>>input;
std::unordered_map<std::string, int>::iterator it = myMap.find(input);
if(it != myMap.end())
std::cout<<"Found";
else
std::cout<<"Not Found";
}
#包括
#包括
int main(){
std::无序图myMap;
插入({“asd”,1});
插入({“qwe”,2});
std::字符串输入;
std::coutinput;
std::无序_映射::迭代器it=myMap.find(输入);
if(it!=myMap.end())
std::coutwell,我猜值类型在这里并不重要。我想使用,尽管引用的链接使用。无论哪种方式,key_equal_fn都应该处理string和string,因为它们是散列映射中两个项的键。你是说你想做一个完全专用的模板吗?我会重新编辑它。请编辑你的问题以包含Ca无法复制:
template <typename T>
struct key_equal_fn {
bool operator()(const T& t1, const T& t2) const {
return t1==t2;
}
};
template<>
struct key_equal_fn<string> {
bool operator()(const string& t1, const string& t2) const {
return !(t1.compare(t2));
}
};
unordered_map<string, string, hashing_func, key_equal_fn<string> > text2cnt;
unordered_map<string, string, hashing_func, key_equal_fn<string> >::iterator iter;
#include <iostream>
#include<unordered_map>
int main() {
std::unordered_map<std::string, int> myMap;
myMap.insert({"asd", 1});
myMap.insert({"qwe", 2});
std::string input;
std::cout<<"Enter String: ";
std::cin>>input;
std::unordered_map<std::string, int>::iterator it = myMap.find(input);
if(it != myMap.end())
std::cout<<"Found";
else
std::cout<<"Not Found";
}