C++ Hashset搜索函数不工作C++;
我正在尝试实现一个hashset,显然我需要一个搜索函数。我做了一个,但似乎不起作用。我想在哈希集中添加人。以下是所有代码:C++ Hashset搜索函数不工作C++;,c++,hashset,C++,Hashset,我正在尝试实现一个hashset,显然我需要一个搜索函数。我做了一个,但似乎不起作用。我想在哈希集中添加人。以下是所有代码: /* * HSet.h */ #ifndef HSET_H_ #define HSET_H_ #include <cstdio> template <typename Element> class MyHashSet{ private: class Node{ private: Element info;
/*
* 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_ */
我在搜索功能中发现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;
}
模板
int-MyHashSet::searchElementInTheSet(长整型,元素e){
int index=hashFunction(nr);
Node*cursor=this->head[索引];
而((游标->getNext()!=NULL)和(游标->getInfo()!=e)){//错误:此行有多个标记
-“MyHashSet::Node::getInfo()[与元素=
人]()!=e'
-候选者是错误结束者
游标=游标->获取下一步();
}
if(cursor->getInfo()==e){//错误:此行有多个标记
-“MyHashSet::Node::getInfo()[with Element=Person]中的”operator==”不匹配
()==e'
-候选者是错误结束者
返回1;
}
返回0;
}
如何打印哈希集中的所有元素?我就是想不出来。所以这里:
while((cursor->getNext()!=NULL) and(cursor->getInfo()!=e)){
“operator!=”不匹配在'MyHashSet::Node::getInfo()中,元素=
人!=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==
}
while((cursor->getNext()!=NULL) and(cursor->getInfo()!=e)){
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==
}