Solved

how do i declare an unorder set with my own hash function C++

Posted on 2014-11-24
1
251 Views
Last Modified: 2014-11-25
I wan to use the STL unordered_set in C++ with a hash function I wrote. How do I declare that?
0
Comment
Question by:pcomb
1 Comment
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40463521
I think you have two options: create a function object (functor) or use std::function() template to pass your function to the constructor of the unordered_set. For the former just use the following code:
struct my_hash {
        size_t operator () (const myClass &f) const { return myClass.hash_value; }
};
unordered_set<myClass, my_hash> s;

Open in new window

for the latter:
size_t my_hash_function() (const myClass &f) const { return myClass.hash_value; }
};
unordered_set<myClass, std::function<decltype(my_hash_function)>> s;

Open in new window

0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question