This commit is contained in:
krahets
2023-02-08 04:17:26 +08:00
parent 7f4efa6d5e
commit 0407cc720c
347 changed files with 150 additions and 132904 deletions

View File

@@ -424,54 +424,9 @@ $$
=== "C++"
```cpp title="array_hash_map.cpp"
/* 键值对 int->String */
struct Entry {
public:
int key;
string val;
Entry(int key, string val) {
this->key = key;
this->val = val;
}
};
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private:
vector<Entry*> bucket;
public:
ArrayHashMap() {
// 初始化一个长度为 100 的桶(数组)
bucket= vector<Entry*>(100);
}
/* 哈希函数 */
int hashFunc(int key) {
int index = key % 100;
return index;
}
/* 查询操作 */
string get(int key) {
int index = hashFunc(key);
Entry* pair = bucket[index];
return pair->val;
}
/* 添加操作 */
void put(int key, string val) {
Entry* pair = new Entry(key, val);
int index = hashFunc(key);
bucket[index] = pair;
}
/* 删除操作 */
void remove(int key) {
int index = hashFunc(key);
// 置为 nullptr ,代表删除
bucket[index] = nullptr;
}
};
[class]{Entry}-[func]{}
[class]{ArrayHashMap}-[func]{}
```
=== "Python"