From cf0d4b32ec00cca8bf421c4670dc11ff99aeb5b3 Mon Sep 17 00:00:00 2001 From: XiaChuerwu <93534259+XiaChuerwu@users.noreply.github.com> Date: Tue, 29 Aug 2023 20:24:42 +0800 Subject: [PATCH] Added destructor function (#702) * Added destructor function * Fixed hash function * Added hash_map_chaining.c file * Amendment submission * Delete hash_map_chaining.c --------- Co-authored-by: Yudong Jin --- codes/cpp/chapter_hashing/hash_map_chaining.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/codes/cpp/chapter_hashing/hash_map_chaining.cpp b/codes/cpp/chapter_hashing/hash_map_chaining.cpp index ef8fc11d5..012ad9abc 100644 --- a/codes/cpp/chapter_hashing/hash_map_chaining.cpp +++ b/codes/cpp/chapter_hashing/hash_map_chaining.cpp @@ -20,6 +20,16 @@ class HashMapChaining { HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3), extendRatio(2) { buckets.resize(capacity); } + + /* 析构方法 */ + ~HashMapChaining() { + for (auto &bucket : buckets) { + for (Pair *pair : bucket) { + // 释放内存 + delete pair; + } + } + } /* 哈希函数 */ int hashFunc(int key) {