ハッシュセットを実装しようとしていますが、明らかに検索機能が必要です。作ったのですがうまくいかないようです。ハッシュセットに人物を追加したい。すべてのコードは次のとおりです。
/*
* 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;
}
ハッシュセットからすべての要素を出力するにはどうすればよいですか? 私はそれを理解することはできません。