gpt4 book ai didi

c++ - 调用哈希类函数

转载 作者:行者123 更新时间:2023-11-28 02:36:44 25 4
gpt4 key购买 nike

<分区>

我正在尝试创建自己的哈希类。然而,经过无数次尝试,我无法让我的程序正常工作。我相信调用函数时有错误,但我不太确定。谁能帮我弄清楚我做错了什么,并可能告诉我如何解决它?

hash.h

#include <iostream>

class MyHash {
public:
MyHash();
int hashCode(char, char);
};

hash.cpp

#include <iostream>
#include "Hash.h"

MyHash::MyHash() {}

int MyHash::hashCode(char first_letter, char last_letter) {
int hash = 0;
int ascii = 1;

hash = ((ascii * first_letter) + (ascii * last_letter)) % 23;

return (hash);
}

驱动.cpp

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "Hash.h"

using namespace std;

int main() {
vector<string> words;
int first_letter;
int last_letter;
string word;
int n = 0;
int hash = 0;

for (int i = 0; i < 5; i++) {
cout << " Please enter a word: ";
cin >> word;
words.push_back(word);
}

vector<string>::iterator itr = words.begin();
for (; itr != words.end(); itr++) {
first_letter = *itr[n].begin();
last_letter = *itr[n].rbegin();
cout << endl << " " << first_letter << " " << last_letter << endl;
hash = hashCode(first_letter, last_letter) cout << hash << endl;
}
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com