实现C++经典hashmap

C++经典hash是指使用哈希表实现的一种数据结构,可以快速地存储、查找和删除数据。在C++中,可以使用STL中的unordered_map或者手写哈希表来实现。

关于C++经典hash的一些知识点包括:

哈希函数:将关键字映射到哈希表中的索引位置的函数。常见的哈希函数包括取模法、乘法哈希、MD5等。

哈希冲突:当两个不同的关键字被映射到同一个哈希表索引位置时,就会发生哈希冲突。解决哈希冲突的方法有开放地址法、链表法、再哈希法等。

#include <iostream>
#include <string>

using namespace std;

const int TABLE_SIZE = 100;

class HashNode {
public:
    string key;
    int value;
    HashNode* next;

    HashNode(string key, int value) {
        this->key = key;
        this->value = value;
        this->next = nullptr;
    }
};

class HashMap {
private:
    HashNode** table;
public:
    HashMap() {
        table = new HashNode*[TABLE_SIZE];
        for (int i = 0; i < TABLE_SIZE; i++) {
            table[i] = nullptr;
        }
    }

    ~HashMap() {
        for (int i = 0; i < TABLE_SIZE; i++) {
            HashNode* entry = table[i];
            while (entry != nullptr) {
                HashNode* prev = entry;
                entry = entry->next;
                delete prev;
            }
            table[i] = nullptr;
        }
        delete[] table;
    }

    int hash(string key) {
        int hashVal = 0;
        for (int i = 0; i < key.length(); i++) {
            hashVal = 37 * hashVal + key[i];
        }
        hashVal %= TABLE_SIZE;
        if (hashVal < 0) {
            hashVal += TABLE_SIZE;
        }
        return hashVal;
    }

    void insert(string key, int value) {
        int hashVal = hash(key);
        HashNode* prev = nullptr;
        HashNode* entry = table[hashVal];
        while (entry != nullptr && entry->key != key) {
            prev = entry;
            entry = entry->next;
        }
        if (entry == nullptr) {
            entry = new HashNode(key, value);
            if (prev == nullptr) {
                table[hashVal] = entry;
            } else {
                prev->next = entry;
            }
        } else {
            entry->value = value;
        }
    }

    int get(string key) {
        int hashVal = hash(key);
        HashNode* entry = table[hashVal];
        while (entry != nullptr && entry->key != key) {
            entry = entry->next;
        }
        if (entry == nullptr) {
            return -1;
        } else {
            return entry->value;
        }
    }

    void remove(string key) {
        int hashVal = hash(key);
        HashNode* prev = nullptr;
        HashNode* entry = table[hashVal];
        while (entry != nullptr && entry->key != key) {
            prev = entry;
            entry = entry->next;
        }
        if (entry == nullptr) {
            return;
        } else {
            if (prev == nullptr) {
                table[hashVal] = entry->next;
            } else {
                prev->next = entry->next;
            }
            delete entry;
        }
    }
};

int main() {
    HashMap myMap;

    // 插入数据
    myMap.insert("apple", 1);
    myMap.insert("banana", 2);
    myMap.insert("orange", 3);

    // 查找数据
    cout << "apple: " << myMap.get("apple") << endl;

    // 删除数据
    myMap.remove("banana");

    return 0;
}

该示例代码中使用了链表法解决哈希冲突。如果发生哈希冲突,就将新的节点插入到链表的末尾。在查找时,遍历链表查找对应的节点。在删除时,将链表中对应的节点删除即可。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值