在unordered_map上构造的混合链表
hybrid linked list constructed on 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;
优化哈希查找的一种方法是找到一个哈希函数,该函数在您将要使用的键上产生最小数量的哈希冲突。
使用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(),它们将按照指定的顺序排列,请参见对该问题的接受答案
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 打印所有链表的元素 (C++)
- 错误:需要类名(链表c++)
- 为什么C中的通用链表中存储的数据已损坏
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表c++插入,所有情况都已检查,但没有任何工作
- 链表,将列表复制到另一个列表
- 实现 CRTP 链表混合C++
- 在unordered_map上构造的混合链表