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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:59:33+00:00 2026-05-31T17:59:33+00:00

using c++ std’s unordered_map i want to map an integer triplet to a single

  • 0

using c++ std’s unordered_map i want to map an integer triplet to a single integer, i usually don’t use hash tables(didn’t know they were so cool), but i don’t know the right approach in this case, using the default hashing function should i map the triplets directly (something like < < int,int >,int >->int)

std::unordered_map <std::make_pair <make_pair <int,int>,int>,int> hash;

or maybe use a function to map the triplet to a single value and the use that value with the default function?

int mapping(int a, int b, int c){
}

std::unordered_map <int,int> hash;

both approaches work but i’d like to know wich one is the most efficient one. thank you

  • 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-31T17:59:34+00:00Added an answer on May 31, 2026 at 5:59 pm

    First off, you would use std::tuple<int, int, int> as the key type.

    Next, you need a way to hash a tuple given that you can hash each element. There is a function called hash_combine in Boost that does that, but for reasons unclear to me, that one was not included in the standard. Anyway, here it goes:

    #include <tuple>
    #include <utility>
    
    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);
    }
    
    template <class Tuple, std::size_t Index = std::tuple_size<Tuple>::value - 1>
    struct tuple_hash_impl
    {
        static inline void apply(std::size_t & seed, Tuple const & tuple)
        {
            tuple_hash_impl<Tuple, Index - 1>::apply(seed, tuple);
            hash_combine(seed, std::get<Index>(tuple));
        }
    };
    
    template <class Tuple>
    struct tuple_hash_impl<Tuple, 0>
    {
        static inline void apply(std::size_t & seed, Tuple const & tuple)
        {
            hash_combine(seed, std::get<0>(tuple));
        }
    };
    
    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;
            }
        };
    
        template<typename ...Args> struct hash<tuple<Args...>>
        {
            inline size_t operator()(const tuple<Args...> & v) const
            {
                size_t seed = 0;
                tuple_hash_impl<tuple<Args...>>::apply(seed, v);
                return seed;
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using std::map to implement my local hash table, which will be accessed
I'm using std::map to store a lot of elements (pairs of elements) and I
I have written a class using std::tr1::regex, and I don't know how to link
I want to sort a vector using std::sort, but my sort method is a
I am using std::wstring as my Unicode style string. Now I want to get
When using std::map in c++, is it possible to store inherited classes as their
I am using std::unordered_map and was trying to get the memory consumption so I
I have some questions on using std::map : Is using an enum as the
I want to make a simple text editor using std::strings. If my text is
I have a function that translates data using std::map struct HistoParameter { int nbins;

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.