Suppose I have an std::unordered_set<T> mySet
initialized with my own hash function hashFunc(T item)
. What I want to do is first insert some items into mySet
, and then have a function search(T item)
that takes an item, finds which bucket b
it would go if it were to be inserted (but does not insert it) and finally returns all other items on bucket b
. Can I calculate b
like this?
b = hashFunc(item)
Is it guaranteed that b
is gonna be the bucket I'm looking for? If not, what alternatives do I have?
The bucket method on
unordered_set
takes a key and returns the bucket index that an element with that key would go into. For example:on my implementation, prints
Once you have the bucket index, you would then iterate over the items in that bucket by calling the begin, end overloads that take a bucket index.
You shouldn't need to directly call your hash function.