我是一名 C++ 学生,我只是想学习如何创建哈希表。我有哈希编码部分,但我不知道如何将表格实现到编码部分。如果有人可以给我帮助,那就太好了。谢谢
Movies::Movies(string fn){loadMovies(fn);}
int Movies::getMovieCount() const {return movieCnt;}
int hashcode(string title)
{
int hash = 0;
int factor = 0 ;
for (int i = 0; i<title.length(); i++)
hash = factor* title.at(i);
return hash;
}
const Movie * Movies::getMovie(string mc, int& mn) const {
if(mc.length()==0)
return NULL; // not found
else {
string mcP = myToLower(mc);
int ndx=0;
for(;ndx<movieCnt &&
(myToLower(movies[ndx].getTitle()).find(mcP)==
string::npos);ndx++);
mn = ndx<movieCnt?ndx+1:0;
return ndx<movieCnt?&movies[ndx]:NULL;
}
}
Movies::~Movies() {
delete[] movies;
movies = NULL;
}
void Movies::loadMovies(string fn) {
ifstream iS(fn);
string s;
getline(iS, s); // skip heading
getline(iS, s);
movieCnt=0;
movies = new Movie[MAX_MOVIES];
while(!iS.eof()) {
movies[movieCnt++] = Movie(s);
getline(iS, s);
}
iS.close();
reSize();
}
void Movies::reSize() {
Movie * m = movies;
movies = new Movie[movieCnt];
for(int i=0;i<movieCnt;i++)
movies[i] = m[i];
}