<分区>
问题:
Write a program that implements a hash function on student roll no and categorize them in their families. Like 5000423, last 2 digits 23, 2+3=5, so belongs to family 5.
我的尝试:
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
const int tablesize= 20;
class hashentry
{
public:
int key;
int value;
hashentry(int key,int value)
{
this->key=key;
this->value=value;
}
};
class hashmap
{
public:
hashentry **table;
public:
hashmap()
{
int table=new hashentry *[tablesize];
for(int i=0;i<tablesize;i++)
{
table[i]=NULL;
}
}
int hashfunc(int key)
{
return key%tablesize;
}
void insert(int key, int value)
{
int hash=hashfunc(key);
while(table[hash]!=NULL && table[hash]->key!=key)
{
hash=hashfunc(hash+1);
}
table[hash]=new hashentry(key,value);
}
};
int main()
{
int key;
int value;
hashmap hash;
cout<<"enter value";
cin>>value;
cout<<"enter key at which element is to be inserted";
cin>>key;
hash.insert(key,value);
return 0;
}
捕获的错误:
In constructor 'hashmap::hashmap()':
invalid conversion from 'hashentry**' to 'int'
invalid types 'int[int]' for array subscript