2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2022-12-14 17:18:32 +08:00
|
|
|
* File: array_hash_map.cpp
|
|
|
|
* Created Time: 2022-12-14
|
|
|
|
* Author: msk397 (machangxinq@gmail.com)
|
|
|
|
*/
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
#include "../utils/common.hpp"
|
2022-12-14 17:18:32 +08:00
|
|
|
|
|
|
|
/* 键值对 int->String */
|
|
|
|
struct Entry {
|
2023-04-14 03:44:02 +08:00
|
|
|
public:
|
2022-12-14 17:18:32 +08:00
|
|
|
int key;
|
|
|
|
string val;
|
|
|
|
Entry(int key, string val) {
|
|
|
|
this->key = key;
|
|
|
|
this->val = val;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
|
|
|
class ArrayHashMap {
|
2023-04-14 03:44:02 +08:00
|
|
|
private:
|
|
|
|
vector<Entry *> buckets;
|
|
|
|
|
|
|
|
public:
|
2022-12-14 17:18:32 +08:00
|
|
|
ArrayHashMap() {
|
2023-03-15 03:48:31 +08:00
|
|
|
// 初始化数组,包含 100 个桶
|
2023-04-14 03:44:02 +08:00
|
|
|
buckets = vector<Entry *>(100);
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
2023-04-15 23:51:35 +08:00
|
|
|
~ArrayHashMap() {
|
|
|
|
// 释放内存
|
|
|
|
for (const auto &bucket : buckets) {
|
|
|
|
delete bucket;
|
|
|
|
}
|
|
|
|
buckets.clear();
|
|
|
|
}
|
|
|
|
|
2022-12-14 17:18:32 +08:00
|
|
|
/* 哈希函数 */
|
|
|
|
int hashFunc(int key) {
|
|
|
|
int index = key % 100;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
string get(int key) {
|
|
|
|
int index = hashFunc(key);
|
2023-04-14 03:44:02 +08:00
|
|
|
Entry *pair = buckets[index];
|
2023-02-08 04:17:26 +08:00
|
|
|
if (pair == nullptr)
|
|
|
|
return nullptr;
|
2022-12-15 09:43:20 +08:00
|
|
|
return pair->val;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
void put(int key, string val) {
|
2023-04-14 03:44:02 +08:00
|
|
|
Entry *pair = new Entry(key, val);
|
2022-12-14 17:18:32 +08:00
|
|
|
int index = hashFunc(key);
|
2023-03-15 03:11:43 +08:00
|
|
|
buckets[index] = pair;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
void remove(int key) {
|
|
|
|
int index = hashFunc(key);
|
2023-04-15 23:51:35 +08:00
|
|
|
// 释放内存并置为 nullptr
|
|
|
|
delete buckets[index];
|
2023-03-15 03:11:43 +08:00
|
|
|
buckets[index] = nullptr;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有键值对 */
|
2023-04-14 03:44:02 +08:00
|
|
|
vector<Entry *> entrySet() {
|
|
|
|
vector<Entry *> entrySet;
|
|
|
|
for (Entry *pair : buckets) {
|
2022-12-15 09:43:20 +08:00
|
|
|
if (pair != nullptr) {
|
2022-12-14 17:18:32 +08:00
|
|
|
entrySet.push_back(pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entrySet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有键 */
|
|
|
|
vector<int> keySet() {
|
|
|
|
vector<int> keySet;
|
2023-04-14 03:44:02 +08:00
|
|
|
for (Entry *pair : buckets) {
|
2022-12-15 09:43:20 +08:00
|
|
|
if (pair != nullptr) {
|
|
|
|
keySet.push_back(pair->key);
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return keySet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有值 */
|
|
|
|
vector<string> valueSet() {
|
|
|
|
vector<string> valueSet;
|
2023-04-14 03:44:02 +08:00
|
|
|
for (Entry *pair : buckets) {
|
|
|
|
if (pair != nullptr) {
|
2022-12-15 09:43:20 +08:00
|
|
|
valueSet.push_back(pair->val);
|
|
|
|
}
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
return valueSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
|
|
|
void print() {
|
2023-04-14 03:44:02 +08:00
|
|
|
for (Entry *kv : entrySet()) {
|
2022-12-15 09:43:20 +08:00
|
|
|
cout << kv->key << " -> " << kv->val << endl;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-12-15 23:08:07 +08:00
|
|
|
|
|
|
|
/* Driver Code */
|
2022-12-14 17:18:32 +08:00
|
|
|
int main() {
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
ArrayHashMap map = ArrayHashMap();
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
map.put(12836, "小哈");
|
|
|
|
map.put(15937, "小啰");
|
|
|
|
map.put(16750, "小算");
|
|
|
|
map.put(13276, "小法");
|
|
|
|
map.put(10583, "小鸭");
|
|
|
|
cout << "\n添加完成后,哈希表为\nKey -> Value" << endl;
|
|
|
|
map.print();
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
string name = map.get(15937);
|
|
|
|
cout << "\n输入学号 15937 ,查询到姓名 " << name << endl;
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
map.remove(10583);
|
|
|
|
cout << "\n删除 10583 后,哈希表为\nKey -> Value" << endl;
|
|
|
|
map.print();
|
|
|
|
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
cout << "\n遍历键值对 Key->Value" << endl;
|
2023-04-14 03:44:02 +08:00
|
|
|
for (auto kv : map.entrySet()) {
|
2022-12-15 09:43:20 +08:00
|
|
|
cout << kv->key << " -> " << kv->val << endl;
|
2022-12-14 17:18:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cout << "\n单独遍历键 Key" << endl;
|
2023-04-14 03:44:02 +08:00
|
|
|
for (auto key : map.keySet()) {
|
2022-12-14 17:18:32 +08:00
|
|
|
cout << key << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "\n单独遍历值 Value" << endl;
|
2023-04-14 03:44:02 +08:00
|
|
|
for (auto val : map.valueSet()) {
|
2022-12-14 17:18:32 +08:00
|
|
|
cout << val << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2023-04-15 23:51:35 +08:00
|
|
|
}
|