哈希集搜索功能不适用于C ++

时间:2013-05-07 17:05:28

标签: c++ hashset

我正在尝试实现一个hashset,我显然需要一个搜索功能。我做了一个,但似乎没有用。我想在hashset中添加Persons。以下是所有代码:

/*
 * 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;
}

如何打印哈希集中的所有元素?我只是想不出来。

1 个答案:

答案 0 :(得分:0)

所以这里:

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

在'MyHashSet :: Node :: getInfo()中使用Element =与'operator!='不匹配    人!= 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==
}