简体   繁体   English

std::unordered_map 插入提示

[英]std::unordered_map insert with hint

std::map has an insert method that takes a "hint" iterator that will reduce the insertion time from log(n) to constant time if the hint is correct. std::map有一个insert方法,它接受一个“提示”迭代器,如果提示正确,它将把插入时间从 log(n) 减少到常数时间。 Its pretty obvious how this would work, since the container could just make sure the newly added item has a key that is less than the hint and has a key that is greater than the item before the hint.很明显这是如何工作的,因为容器可以确保新添加的项目具有小于提示的键并且具有大于提示之前的项目的键。 Otherwise the hint was wrong and it performs a normal insert.否则提示是错误的,它执行正常插入。

std::unordered_map also has a similar insert with hint function. std::unordered_map也有一个类似的带有提示功能的insert What, if anything, does the hint do?如果有的话,提示有什么作用? Its not obvious to me how another a "hint" iterator could be used to speed up a hash map insertion.对我来说如何使用另一个“提示”迭代器来加速哈希映射插入并不明显。

If it is used, what is an appropriate "hint".如果使用它,什么是适当的“提示”。 In std::map , the hint is typically found by calling lower_bound on the map.std::map ,通常通过在地图上调用lower_bound来找到提示。

It is an interface compatibility issue.这是一个接口兼容性问题。 Basically, the design is done considering the interface of std::map .基本上,设计是考虑到std::map的接口完成的。

In other words, for std::unordered_map it does not differ a hint is provided or not.换句话说,对于std::unordered_map它没有区别提供或不提供提示。

Additional Information from the comments here:来自此处评论的其他信息:

The interface compatibility is very important because being able to quickly/easily switch between map and unordered_map provides the valuable flexibility of painlessly transition since performance is often the deciding factor in choosing one over the other.接口兼容性非常重要,因为能够在mapunordered_map之间快速/轻松地切换提供了无痛转换的宝贵灵活性,因为性能通常是选择其中之一的决定性因素。

The hint allows the unordered map implementation to do a value compare first to see if the hint works.提示允许无序映射实现首先进行值比较以查看提示是否有效。 This avoids having to do the hash function which can be more costly than a compare operation.这避免了必须执行可能比比较操作更昂贵的散列函数。

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

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