简体   繁体   English

我不明白std :: tr1 :: unordered_map

[英]I don't understand std::tr1::unordered_map

I need an associative container that makes me index a certain object through a string, but that also keeps the order of insertion, so I can look for a specific object by its name or just iterate on it and retrieve objects in the same order I inserted them. 我需要一个关联容器,它让我通过一个字符串索引一个特定的对象,但这也保持了插入的顺序,所以我可以通过它的名字查找一个特定的对象,或者只是迭代它并按照我插入的相同顺序检索对象他们。

I think this hybrid of linked list and hash map should do the job, but before I tried to use std::tr1::unordered_map thinking that it was working in that way I described, but it wasn't. 我认为链接列表和哈希映射的这种混合应该可以完成这项工作,但在我尝试使用std::tr1::unordered_map之前,我认为它是以我所描述的方式工作,但事实并非如此。 So could someone explain me the meaning and behavior of unordered_map ? 那么有人可以解释一下unordered_map的含义和行为吗?


@wesc: I'm sure std::map is implemented by STL, while I'm sure std::hash_map is NOT in the STL (I think older version of Visual Studio put it in a namespace called stdext). @wesc:我确定std :: map是由STL实现的,而我确定std :: hash_map不在STL中(我认为旧版本的Visual Studio将它放在名为stdext的命名空间中)。

@cristopher: so, if I get it right, the difference is in the implementation (and thus performances), not in the way it behaves externally. @cristopher:所以,如果我做对了,差异在于实现(以及性能),而不是它在外部的行为方式。

You've asked for the canonical reason why Boost::MultiIndex was made: list insertion order with fast lookup by key. 您已经询问了为什么制作Boost :: MultiIndex的规范原因:列出按键快速查找的插入顺序。 Boost MultiIndex tutorial: list fast lookup Boost MultiIndex教程:列出快速查找

You need to index an associative container two ways: 您需要以两种方式索引关联容器:

  • Insertion order 广告订单
  • String comparison 字符串比较

Try Boost.MultiIndex or Boost.Intrusive . 尝试Boost.MultiIndexBoost.Intrusive I haven't used it this way but I think it's possible. 我没有用这种方式,但我认为这是可能的。

Boost documentation of unordered containers 提升无序容器的文档

The difference is in the method of how you generate the look up. 不同之处在于如何生成查找的方法。

In the map/set containers the operator< is used to generate an ordered tree. 在map / set容器中, operator<用于生成有序树。

In the unordered containers, an operator( key ) => index is used. 在无序容器中,使用operator( key ) => index

See hashing for a description of how that works. 有关其工作原理的说明,请参见散列。

Sorry, read your last comment wrong. 对不起,请阅读您的上次评论错误。 Yes, hash_map is not in STL, map is. 是的,hash_map不在STL中,map是。 But unordered_map and hash_map are the same from what I've been reading. 但unordered_map和hash_map与我读过的内容完全相同。

map -> log (n) insertion, retrieval, iteration is efficient (and ordered by key comparison) map - > log(n)插入,检索,迭代是有效的(并通过密钥比较排序)

hash_map/unordered_map -> constant time insertion and retrieval, iteration time is not guarantee to be efficient hash_map / unordered_map - >常量时间插入和检索,迭代时间不保证高效

Neither of these will work for you by themselves, since the map orders things based on the key content, and not the insertion sequence (unless your key contains info about the insertion sequence in it). 这些都不适合你自己,因为地图根据关键内容而不是插入顺序排序事物(除非你的密钥包含有关其中插入序列的信息)。

You'll have to do either what you described (list + hash_map), or create a key type that has the insertion sequence number plus an appropriate comparison function. 您必须执行您所描述的内容(list + hash_map),或创建具有插入序列号加上适当比较函数的密钥类型。

I think that an unordered_map and hash_map are more or less the same thing. 认为 unordered_map和hash_map或多或少是相同的。 The difference is that the STL doesn't officially have a hash_map (what you're using is probably a compiler specific thing), so unordered_map is the fix for that omission. 区别在于STL没有正式拥有hash_map(你正在使用的东西可能是编译器特定的东西),所以unordered_map是该遗漏的修复。

unordered_map is just that... unordered. unordered_map就是......无序的。 You can't depend on it preserving any ordering on iteration. 你不能依赖它来保留迭代的任何顺序。

You sure that std::hash_map exists in all STL implementations? 您确定std :: hash_map存在于所有 STL实现中吗? SGI STL implements it, however GNU g++ doesn't have it (it's located in the __gnu_cxx namespace) as of 4.3.1 anyway. SGI STL实现了它,但是无论如何,GNU g ++都没有它(它位于__gnu_cxx命名空间中)。 As far as I know, hash_map has always been non-standard, and now tr1 is fixing that. 据我所知,hash_map一直是非标准的,现在tr1正在修复它。

@wesc: STL has std::map... so what's the difference with unordered_map? @wesc:STL有std :: map ...那么与unordered_map的区别是什么? I don't think STL would implement twice the same thing and call it differently. 我不认为STL会实现两次相同的东西,并以不同的方式调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM