I'm lost on this topic I have been studying. In my class we are implementing our own hash set class. Thus we have an underlying data structure , like a vector or array , and use a hash function to quickly determine whether an element is in the set it not . That is the part I do not follow. How would a hash function be used for this determination ?
Asked
Active
Viewed 1,105 times
0
-
The hash function tells you what bucket the element would be found in. – John Dibling Oct 22 '14 at 20:15
1 Answers
0
imagine you have an underlying array of size 100, and you can only insert values from 0 to 99.
something like this:
class UselessHashMap
{
public:
void insert(int value){
_arr[hash(i)] = i;
}
private:
int hash(int i) { return i };
std::array<int,100> _arr;
}
now, imagine you want to store more than 100 elements, and you can't have an array that has an infinite (std::numeric_limits::max() )size. In this case, your hash function will have to return you a value between 0-99, and of course your UselessHashMap class will need to take care of collisions as well, because that function could return the same value for different inputs.

dau_sama
- 4,247
- 2
- 23
- 30