• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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

I wan to use the STL unordered_set in C++ with a hash function I wrote. How do I declare that?
0
pcomb
Asked:
pcomb
1 Solution
 
chaauCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now