在unordered_map上构造的混合链表

hybrid linked list constructed on unordered_map?

本文关键字:混合 链表 unordered map      更新时间:2023-10-16

嗨,我想知道我是否可以自己设置另一个链接结构来实际设置我自己在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;

优化哈希查找的一种方法是找到一个哈希函数,该函数在您将要使用的键上产生最小数量的哈希冲突。

使用std::unordered_map,您还可以获得局部迭代器到桶并重新排列桶中的元素,如果您愿意的话。

在我看来,一个更好的解决方案是:

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

现在您可以简单地使用该映射的迭代器对begin()/end(),它们将按照指定的顺序排列,请参见对该问题的接受答案