2013-06-20 34 views
2

您好我不知道我是否可以设置另一个链接的结构自己实际上在unordered_map中的键之间设置我自己的顺序?或者有一个标准库?我需要unordered_map的快速查找功能...在unordered_map上构造的混合链表?

例如:

#include<string> 
#include<tr1/unordered_map> 

struct linker 
{ 
    string *pt; 
    string *child1; 
    string *child2; 
}; 

unordered_map<string,int> map({{"aaa",1},{"bbb",2},{"ccc",3},{"ddd",4}}); 

linker node1 = new linker; 
node1.pt = &map.find("aaa")->first; 
node1.child1 = &map.find("ccc")->first; 
node1.child2 = &map.find("ddd")->first; 
+0

存储迭代器而不是指针可能会更好。 – imreal

+0

@Nick,但如果大小改变并重新设置,迭代器将会改变。不是吗? – weeo

+0

有很多事情使迭代器和指针无效,你必须小心不管哪种方式。 – imreal

回答

0

一种方式来优化哈希查找,找到产生上的按键,你哈希冲突的数量最少的哈希函数将要使用。

使用std::unordered_map您还可以get local iterators to buckets并重新排列存储桶中的元素,如果您非常喜欢。

0

一个更好的解决方案恕我直言,将是如下:

struct comparator { 
    bool operator()(string const& lhs, string const& rhs) { 
     return ...;//Your definition of order here!!! 
     } 
}; 

std::map<string, int, comparator> map{{"aaa",1},{"bbb",2},{"ccc",3},{"ddd",4}};//note the elided paranthesis 

现在,你可以简单地使用迭代器对这个地图,这将是在一个特定的顺序的()开始/结束()见对此的接受答案question