Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:30:23+00:00 2026-06-14T15:30:23+00:00

I’m using a std::unordered_set for the first time and have a question about the

  • 0

I’m using a std::unordered_set for the first time and have a question about the hash function. As far as I understand, if you don’t specify a hash function it will default to std::hash<Key>.

I have a mySet member in one of my classes:

typedef std::unordered_set<MyClass> USetType;
USetType mySet;

When I try to build, I get the following error:

error C2440: ‘type cast’ : cannot convert from ‘const MyClass’ to ‘size_t’

Is it necessary to define a conversion function (to size_t) if you want to use unordered_set with a custom class? Is there any way to avoid writing your own hash function and just using the default?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:30:24+00:00Added an answer on June 14, 2026 at 3:30 pm

    If you don’t specify your own hash functor as template argument, it will default to std::hash<MyClass>, which does not exist unless you define it.

    Best define your own specialization of std::hash inside namespace std:

    namespace std {
      template <>
      struct hash<MyClass>
      {
        typedef MyClass      argument_type;
        typedef std::size_t  result_type;
    
        result_type operator()(const MyClass & t) const
        {
           /* ..calculate hash value for t */
        }
      };
    }
    

    And make sure you include this code before the declaration of your hash. This way you can declare the hash simply as std::unordered_set<MyClass> with no need for further template arguments.

    You didn’t specify what MyClass looks like inside, but a typical situation is that your user-defined type simply consists of several simple-type members, for which a default hash function exists. In this case, you will probably want to combine the hash values for the individual types to a hash value for the entire combination. The Boost library provides a function called hash_combine for this purpose. Of course, there is no guarantee that it will work well in your particular case (it depends on the distribution of data values and the likelihood of collisions), but it provides a good and easy-to-use starting point.

    Here is an example of how to use it, assuming MyClass consists of two string members:

    #include <unordered_set>
    #include <boost/functional/hash.hpp>
    
    struct MyClass
    {
      std::string _s1;
      std::string _s2;
    };
    
    namespace std {
      template <>
      struct hash<MyClass>
      {
        typedef MyClass      argument_type;
        typedef std::size_t  result_type;
    
        result_type operator()(const MyClass & t) const
        {
          std::size_t val { 0 };
          boost::hash_combine(val,t._s1);
          boost::hash_combine(val,t._s2);
          return val;
        }
      };
    }
    
    int main()
    {
      std::unordered_set<MyClass> s;
      /* ... */
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.