Solved

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

Posted on 2014-11-24
1
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 25

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

717 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