-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
75 lines (65 loc) · 1.79 KB
/
Copy pathHashTable.cpp
File metadata and controls
75 lines (65 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <vector>
#include <list>
#include <utility>
#include <iostream>
template<typename K, typename V>
class HashTable {
public:
// 构造:默认桶大小
explicit HashTable(size_t bucket = 97) : _buckets(bucket) {}
~HashTable() = default;
HashTable(const HashTable& other) = delete;
HashTable& operator=(const HashTable& other) = delete;
// 增 / 改
void put(const K& key, const V& val) {
size_t idx = hash(key) % _buckets.size();
auto& list = _buckets[idx];
// 查找是否已存在
for (auto& p : list) {
if (p.first == key) {
p.second = val;
return;
}
}
// 不存在则插入
list.emplace_back(key, val);
}
bool get(const K& key, V* out_val) {
if (!out_val) return false;
size_t idx = hash(key) % _buckets.size();
auto& list = _buckets[idx];
for (auto& p : list) {
if (p.first == key) {
*out_val = p.second;
return true;
}
}
return false;
}
bool erase(const K& key) {
size_t idx = hash(key) % _buckets.size();
auto& list = _buckets[idx];
for (auto it = list.begin(); it != list.end(); ++it) {
if (it->first == key) {
list.erase(it);
return true;
}
}
return false;
}
private:
size_t hash(const K& key) const {
return std::hash<K>{}(key);
}
std::vector<std::list<std::pair<K, V>>> _buckets;
};
int main() {
HashTable<int, int> table(11);
table.put(1, 1);
int temp;
if (table.get(1, &temp)) {
std::cout << "found value: " << temp;
} else {
std::cout << "not found";
}
}