0

I am trying to implement a hash set but I have some trouble for the hash function. I want to add in the set, persons that have name and phone number:

class Person{
    string name;
    long long int phoneNumber;
}

And my indexes in the set are calculated by summing the digits of the phoneNumber. The problem is that I dont want my functions to be something like this:

int add(long long int nr, Element e) - the function that adds an Element to the set
{
     int hashCode = hash(nr);;
     ...
}

where the long long int nr should be the phoneNumber and Element e should be the Person. I mean, it's pretty stupid. If I already have the person as parameter, why have it's phoneNumber too? As you can see I am using templates, and my teacher advised me to do a virtual class for the hashFunction to force it to be the respective type(something like the HashSet in Java). The thing is I have NO IDEA how to do that. Do you have any ideas that could help me?

4

1 回答 1

0

如果你这样做:

 int add(Person p, Element e)

您将限制您的 hastset 类完全依赖于Person类。以这种方式将整数或字符串值带入哈希方法更合理。您可以在散列方法之外确定要散列的参数并将其作为输入。

此外,当您在 Person 类中添加另一个成员(例如 age 并在哈希中使用 in)时,您还需要修改 hash 方法以使用该特定成员。

于 2013-05-18T15:25:56.253 回答