简体   繁体   中英

boost::unordered_map — Need to specify a custom hash function for hashing std::set<int>?

I'd like use boost::unordered_map<key,value> , where key is a std::set<int> . Since a set of integers is no built-in type, I assumed I had to supply my own hash function (or, rather, I was thinking of using boost's hash_range ).

However, now I tried initializing a hash map like this, neither supplying a hash function nor an equality predicate -- and gcc didn't complain. What is happening here? Is boost clever enough to hash STL containers all on its own? Is this going to be slower than if I used a custom hash function? What about using boost::hash_range ?

Thanks in advance.

According to the Boost documentation :

the default hash function is Boost.Hash

And, according to the documentation for Boost.Hash , default hash functions are provided for the standard containers. Consequently, there is already a hash function written for std::set . The Boost hash containers aren't smart enough to know how to hash sets automatically, but they are smart enough to use the implementation that's already provided.

Hope this helps!

The default boost::hash< Key > function is being chosen. According to its documentation

As it is compliant with TR1, it will work with:

 integers floats pointers strings 

It also implements the extension proposed by Peter Dimov in issue 6.18 of the Library Extension Technical Report Issues List (page 63), this adds support for:

 arrays std::pair the standard containers. extending boost::hash for custom types. 

http://www.boost.org/doc/html/hash.html

So yes, boost is clever enough to hash STL containers. Unless you know something specific of your particular use case of set , I doubt there is any point in providing your own hash function.

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