0

我正在尝试实现一个哈希集,我显然需要一个搜索功能。我做了一个,但它似乎不起作用。我想在哈希集中添加人员。这是所有代码:

/*
 * HSet.h
 */

#ifndef HSET_H_
#define HSET_H_
#include <cstdio>

template <typename Element>
class MyHashSet{
private:
    class Node{
    private:
        Element info;
        Node* next;
    public:
        Node(){
            this->next=NULL;
        }
        Node(Element info, Node* next){
            this->info=info;
            this->next=next;
        }
        Node(const Node& node){
            this->info=node.info;
            this->next=node.next;
        }
        ~Node(){}
        Element getInfo(){
            return this->info;
        }
        Node* getNext(){
            return this->next;
        }
        void setNext(Node* value){
            this->next=value;
        }
        void setInfo(Element el){
            this->info=el;
        }
    };
    Node** head;
    int size;
    int* bucketsize;
    int totalElements;
public:
    MyHashSet();
    ~MyHashSet();
    int hashFunction(long long int nr);
    bool isEmpty(int index);
    int searchElementInTheSet(long long int nr, Element e);
    void addElementAtTheEndOfTheSet(long long int nr, Element e);
    int totalElementsInTheSet(){
        return this->totalElements;
    }
    int HashSize(){
        return this->size;
    }
    int bucketNumberOfElements(int index){
        return this->bucketsize[index];
    }
};

template<typename Element>
MyHashSet<Element>::MyHashSet(){
    this->size=11;
    this->head= new MyHashSet::Node*[this->size];
    this->bucketsize= new int[this->size];
    for(int i=0; i < this->size; i++){
        this->head[i]=NULL;
        this->bucketsize[i]=0;
    }
}

template<typename Element>
MyHashSet<Element>::~MyHashSet(){
    delete[] head;
    delete[] bucketsize;
}

template<typename Element>
bool MyHashSet<Element>::isEmpty(int index){
    if(index>=0 and index < this->size){
        return head[index]==NULL;
    }
    return true;
}

template<typename Element>
int MyHashSet<Element>::hashFunction(long long int nr){
    int sum=0;
    int divisor=10;
    while(nr != 0){
        sum+=nr % divisor;
        nr=nr / divisor;
    }
    int hashCode = sum % this->size;
    return hashCode;
}

template<typename Element>
int MyHashSet<Element>::searchElementInTheSet(long long int nr, Element e){
    int index  = hashFunction(nr);
    Node* cursor = this->head[index];
    while((cursor->getNext()!=NULL) and(cursor->getInfo()!=e)){
        cursor = cursor->getNext();
    }
    if(cursor->getInfo()==e){
        return 1;
    }
    return 0;
}

template<typename Element>
void MyHashSet<Element>::addElementAtTheEndOfTheSet(long long int nr, Element e){
    int index = hashFunction(nr);
    Node* add = new Node(e,NULL);
    if(isEmpty(index)){ //if is empty at at the beginning
        this->head[index]=add;
        ++totalElements;
        ++bucketsize[index];
    }
    else{
        Node* cursor = this->head[index];
        while(cursor->getNext() != NULL){
            cursor = cursor->getNext();
        }
        add->setNext(cursor->getNext());
        cursor->setNext(add);
        ++totalElements;
        ++bucketsize[index];
    }
}

#endif /* HSET_H_ */

班级人:

#ifndef PERSON_H_
#define PERSON_H_

#include <string>
using namespace std;

class Person{
private:
    string name;
    long long int phoneNumber;
public:
    Person(){
        this->name="";
        this->phoneNumber=0;
    }
    Person(string name, long long int phoneNumber){
        this->name=name;
        this->phoneNumber=phoneNumber;
    }
    ~Person(){}
    string& getName(){
        return this->name;
    }
    long long int getPhoneNumber(){
        return this->phoneNumber;
    }
    void setName(string newName){
        this->name=newName;
    }
    void setPhoneNumber(long long int newPhoneNumber){
        this->phoneNumber=newPhoneNumber;
    }
};



#endif /* PERSON_H_ */

我试过这个:

Person* p = new Person("asdsads", 1725912975); //4
assert(hash->searchElementInTheSet(p->getPhoneNumber(),*p));

我在搜索功能中遇到了 2 个错误:

template<typename Element>
int MyHashSet<Element>::searchElementInTheSet(long long int nr, Element e){
    int index  = hashFunction(nr);
    Node* cursor = this->head[index];
    while((cursor->getNext()!=NULL) and(cursor->getInfo()!=e)){ //error: Multiple markers at this line
- no match for 'operator!=' in 'MyHashSet<Element>::Node::getInfo() [with Element = 
 Person]() != e'
- candidates are: //error ends
        cursor = cursor->getNext();
    }
    if(cursor->getInfo()==e){ //error: Multiple markers at this line
- no match for 'operator==' in 'MyHashSet<Element>::Node::getInfo() [with Element = Person]
 () == e'
- candidates are: //error ends
        return 1;
    }
    return 0;
}

如何打印哈希集中的所有元素?我就是想不通。

4

1 回答 1

0

所以在这里:

    while((cursor->getNext()!=NULL) and(cursor->getInfo()!=e)){

'MyHashSet::Node::getInfo() 中的 'operator!=' 与 Element = Person != e 不匹配

您实际上是在尝试比较两个 Person 实例——节点中的一个,以及传递给您的搜索功能的一个。您还没有告诉编译器如何比较这种类型的两个实例,但您可以这样做:

bool operator==(Person const &p1, Person const &p2) {
    // return true if they're the same person
    return false;
}
bool operator!=(Person const &p1, Person const &p2) {
    return !(p1 == p2); // just forward to operator==
}
于 2013-05-07T17:19:24.353 回答