简体   繁体   English

std :: unordered_map <int,float>是否仍然需要对整数进行散列才能得到值?

[英]Does std::unordered_map<int,float> still have to hash the integer to get to the value?

I would like to know whether std::unordered_map< int, float > still has to hash the given integer to get to the value or just uses it straight away. 我想知道std::unordered_map< int, float >仍然需要散列给定的整数来获取值或者直接使用它。 I need to perform this operation very fast many times a second and as std::hash<int> isn't guaranteed to be the identity function, how would I go about redefining it? 我需要每秒快速执行此操作很多次,并且由于std::hash<int>不能保证是身份函数,我将如何重新定义它? (obviously not using STL and writing my own containers is possible, but I doubt those written by me will be more efficient (likely much, much slower)). (显然不使用STL并编写我自己的容器是可能的,但我怀疑我写的那些会更有效(可能更多,更慢))。 Thanks! 谢谢!

I would like to know whether std::unordered_map< int, float > still has to hash the given integer 我想知道std::unordered_map< int, float >仍然需要散列给定的整数

Yes it does. 是的,它确实。

I need to perform this operation very fast many times 我需要多次快速执行此操作

Did you complete your project and witnessed that this is the bottleneck of it? 您是否完成了项目并目睹了这是它的瓶颈? If not, then watch out, since you might end up as a victim of premature optimization! 如果没有,那么请注意,因为你可能最终成为过早优化的受害者!

how would I go about redefining it? 我将如何重新定义它?

You have to write your own code then. 你必须编写自己的代码。 Example: C++ unordered_map using a custom class type as the key , where you would use struct Key { int value; }; 示例: 使用自定义类类型作为键的C ++ unordered_map ,您将使用struct Key { int value; }; struct Key { int value; }; .

I very much doubt any sane compiler does it any other way than straight-through. 我非常怀疑任何理智的编译器都是以直通方式进行的。 Even then, you'd have to test to see if it has any actual detrimental performance impact (if they do it, they probably have good reason - eg their hash function is designed to work with their unordered_map). 即便如此,你还是要测试它是否有任何实际的有害性能影响(如果他们这样做,他们可能有充分的理由 - 例如他们的哈希函数被设计为与他们的unordered_map一起工作)。 If you want to force your hashing function, you can have: 如果要强制执行散列函数,可以:

struct Key{
 int value;
};

then look at this answer to check how to make it work with an unordered_map. 然后看看这个答案 ,看看如何使它与unordered_map一起使用。

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

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