?
Solved

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

Posted on 2014-11-24
1
Medium Priority
?
273 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

801 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