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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:42:41+00:00 2026-05-25T00:42:41+00:00

I need to define an unordered_map like this unordered_map<pair<int, int>, *Foo> , what is

  • 0

I need to define an unordered_map like this unordered_map<pair<int, int>, *Foo>, what is the syntax for defining and passing a hash and equal functions to this map?

I’ve tried passing to it this object:

class pairHash{
public:
    long operator()(const pair<int, int> &k) const{
        return k.first * 100 + k.second;
    }
};

and no luck:

unordered_map<pair<int, int>, int> map = unordered_map<pair<int, int>, int>(1,
*(new pairHash()));

I have no Idea what is the size_type_Buskets means so I gave it 1.
What is the right way to do it?
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-25T00:42:42+00:00Added an answer on May 25, 2026 at 12:42 am

    This is an unfortunate omission in C++11; Boost has the answer in terms of hash_combine. Feel free to just paste it from them! Here’s how I hash pairs:

    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);
    }
    
    namespace std
    {
      template<typename S, typename T> struct hash<pair<S, T>>
      {
        inline size_t operator()(const pair<S, T> & v) const
        {
          size_t seed = 0;
          ::hash_combine(seed, v.first);
          ::hash_combine(seed, v.second);
          return seed;
        }
      };
    }
    

    You can use hash_combine as the basis for many other things, like tuples and ranges, so you could hash an entire (ordered) container, for example, as long as each member is individually hashable.

    Now you can just declare a new map:

    std::unordered_map<std::pair<int, int>, my_mapped_type> mymap;
    

    If you want to use your homebrew hasher (which hasn’t got good statistical properties), you have to specify the template parameters explicitly:

    std::unordered_map<std::pair<int,int>, int, pairHash> yourmap;
    

    Note that there’s no need to specify a copy of a hasher object, as the default is to default-construct one for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to define a class like this: class Color { private: union Data
I need to define this function called total. total :: (Int -> Int) ->
I need to define a calculated member in MDX (this is SAS OLAP, but
I need to define multiple modules in the same file. I would like to
I am trying to do the following: boost::unordered_map<boost::flyweight<std::string>, boost::flyweight<std::string> > map; boost::flyweight<std::string> foo(name); map[foo]
I need to define a weak reference Map, whose value is a Set. I
We need to define a conditional compilation symbol in a class library project. This
I need to define a class that represents a real-life event -- like a
I am defining an XSD. I need to define an element which takes date
I need to define a matrix of int as a type. A column or

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.