Search code examples
c++boosthashmapunordered-mapunordered-set

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.


Solution

  • 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!