Solved

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

Posted on 2014-11-24
1
217 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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now