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 6209007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:54:25+00:00 2026-05-24T05:54:25+00:00

I’m using an std::unordered_map<key,value> in my implementation. i will be using any of the

  • 0

I’m using an std::unordered_map<key,value> in my implementation. i will be using any of the STL containers as the key. I was wondering if it is possible to create a generic hash function for any container being used.

This question in SO offers generic print function for all STL containers. While you can have that, why cant you have something like a Hash function that defines everything ? And yeah, a big concern is also that it needs to fast and efficient.

I was considering doing a simple hash function that converts the values of the key to a size_t and do a simple function like this.

Can this be done ?

PS : Please don’t use boost libraries. Thanks.

  • 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-05-24T05:54:27+00:00Added an answer on May 24, 2026 at 5:54 am

    We can get an answer by mimicking Boost and combining hashes.

    Warning: Combining hashes, i.e. computing a hash of many things from many hashes of the things, is not a good idea generally, since the resulting hash function is not “good” in the statistical sense. A proper hash of many things should be build from the entire raw data of all the constituents, not from intermediate hashes. But there currently isn’t a good standard way of doing this.

    Anyway:

    First off, we need the hash_combine function. For reasons beyond my understanding it’s not been included in the standard library, but it’s the centrepiece for everything else:

    template <class T>
    inline void hash_combine(std::size_t & seed, const T & v)
    {
      std::hash<T> hasher;
      seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    

    Using this, we can hash everything that’s made up from hashable elements, in particular pairs and tuples (exercise for the reader).

    However, we can also use this to hash containers by hashing their elements. This is precisely what Boost’s “range hash” does, but it’s straight-forward to make that yourself by using the combine function.

    Once you’re done writing your range hasher, just specialize std::hash and you’re good to go:

    namespace std
    {
      template <typename T, class Comp, class Alloc>
      struct hash<std::set<T, Comp, Alloc>>
      {
        inline std::size_t operator()(const std::set<T, Comp, Alloc> & s) const
        {
          return my_range_hash(s.begin(), s.end());
        }
      };
    
      /* ... ditto for other containers */
    }
    

    If you want to mimic the pretty printer, you could even do something more extreme and specialize std::hash for all containers, but I’d probably be more careful with that and make an explicit hash object for containers:

    template <typename C> struct ContainerHasher
    {
      typedef typename C::value_type value_type;
      inline size_t operator()(const C & c) const
      {
        size_t seed = 0;
        for (typename C::const_iterator it = c.begin(), end = c.end(); it != end; ++it)
        {
          hash_combine<value_type>(seed, *it);
        }
        return seed;
      }
    };
    

    Usage:

    std::unordered_map<std::set<int>, std::string, ContainerHasher<std::set<int>>> x;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I need a function that will clean a strings' special characters. I do NOT
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.