简体   繁体   中英

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. 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? (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)). Thanks!

I would like to know whether std::unordered_map< int, float > still has to hash the given integer

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; }; 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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