Skip to content

Insertion operation in hash_map_open_addressing.cpp #1593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
FreakWwjh opened this issue Dec 16, 2024 · 1 comment
Closed

Insertion operation in hash_map_open_addressing.cpp #1593

FreakWwjh opened this issue Dec 16, 2024 · 1 comment

Comments

@FreakWwjh
Copy link

FreakWwjh commented Dec 16, 2024

在 第六章 哈希表 -> 6.2哈希冲突 -> 6.2.2开放寻址 -> 1.线性探测 :
书中介绍:插入元素: 通过哈希函数计算桶索引,若发现桶内已有元素,则从冲突位置向后线性遍历(步长通常为
),直至找到空桶,将元素插入其中。
但是
在 hash_map_open_addressing.cpp 中:
/* 添加操作 */void put(int key, string val)
该操作似乎无法实现“开放寻址(线性探测)”的功能。
该功能是:插入元素:通过哈希函数计算桶索引,若发现桶内已有元素,则从冲突位置向后线性遍历,直至找到空桶,将元素插入其中。
但这里的put()函数:int index = findBucket(key);// 若找到键值对,则覆盖 val 并返回

@FreakWwjh FreakWwjh changed the title hash_map_open_addressing.cpp中的插入操作似乎无法实现“开放寻址(线性探测)”的功能 问题反馈:hash_map_open_addressing.cpp中的插入操作似乎无法实现“开放寻址(线性探测)”的功能 Dec 22, 2024
@krahets krahets changed the title 问题反馈:hash_map_open_addressing.cpp中的插入操作似乎无法实现“开放寻址(线性探测)”的功能 Insertion operation in hash_map_open_addressing.cpp Jan 13, 2025
@krahets
Copy link
Owner

krahets commented Mar 21, 2025

Hi,如果 key 在哈希表中已经存在,则需要覆盖这个值;只有当 key 在哈希表中不存在时,才需要寻找新的 bucket 放入该 key-val pair(这部分代码在你提到的代码的下面)。

@krahets krahets closed this as completed Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants