我有一个工作的std ::地图类,这是有点慢,所以我想尝试的其它数据的hash_map,在地图的哈希函数的复合键
我的关键是像
typedef struct {
char * name;
int offset;
}position;
的复合数据类型
而为的std ::地图我用下面的部分排序功能
struct cmp_position {
bool operator()(const position& first,const position& second) {
int tmp = std::strcmp(first.name, second.name);
if(tmp!=0)
return tmp<0;
else
return first.offset<second.offset;
}
};
我的地图定义是
typedef std::map<position,int,cmp_position> myMap;
我一直在寻找的__gcc_ext ::的hash_map这是需要可能仅仅是
struct positionEq
{
bool operator()(const position& s1, const position & s2) const
{
return strcmp(s1.name, s2.name) == 0 && (s1.offset==s2.offset) ;
}
};
这应该工作的平等的功能,但我对自己的复合类型的哈希函数有麻烦。 我想我可以做类似
position s;
char buf[100];
snprintf(buf,100,"%s:%d\n",s.name,s.offset);
,但我有胶合一起的问题。
其实地图和哈希映射可能有点矫枉过正,因为我没有使用键的值,我只是使用我的地图来检查存在。
这是我的意图不使用std :: strings。
感谢
编辑:
在上面的例子中,我试图用一个std ::集而不是的std ::地图,和std ::一套既填充一贯慢,查找条目。尽管整体比较如下表所示,但它使用的内存少得多。我试图运行每组10次
Set map
size 1.8gig 3.1gig
pop <15sec <14sec
find <12sec <9sec
我使用的数据集与多于34mio条目,和填充数据结构后,我试图查找所有34个MIO元素。我猜测的结论是,除了保存内存之外,std :: set更差。
请定义“有点慢”。什么是慢?插入?寻找?穿越? – sbi
学习unordered_map比hash_map更好。两者都是散列表,但unordered_map是(或很快将是)标准。 – Steve314
如果你不需要值,可以使用'set/hash_set/unordered_set' –