在 unordered_map 上构建的混合链表? [英] hybrid linked list constructed on unordered_map?

查看:28
本文介绍了在 unordered_map 上构建的混合链表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否可以自己建立另一个链接结构,以实际上在unordered_map的键之间建立自己的顺序?还是有一个标准库?我需要unordered_map的快速查找功能...

Hi I wonder if I can set up another linked struct myself to actually set up my own order between keys in the unordered_map? or there is a standard library? I need the fast look up function of 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;

推荐答案

IMHO更好的解决方案如下:

A far better solution IMHO would be as follows:

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(),它将以指定的顺序在对此

Now you can simply use the iterator pair begin()/end() of this map which will be in a specified order see in the accepted answer to this question

这篇关于在 unordered_map 上构建的混合链表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆