• c++ 哈希表(Hash Table)


            哈希表(Hash Table),也称为散列表,是一种利用哈希函数来实现键值对映射的数据结构。它通过将键转换为索引,然后将键值对存储在相应索引位置的数组中,以实现快速查找、插入和删除操作的数据结构。

    哈希表的主要特点包括:

    1. 使用哈希函数:哈希表利用哈希函数将键转换为索引,使得查找、插入和删除的速度很快,平均时间复杂度为 O(1)。
    2. 冲突处理:由于哈希函数可能将不同的键映射到相同的索引,所以哈希表需要采取冲突处理方法来处理这种情况,常见的方法有开放寻址法和链表法。
    3. 动态扩容:哈希表通常会在达到一定负载因子时进行动态扩容,以保持较低的冲突几率和保持高效率的性能。
    4. 空间效率:哈希表的空间效率较高,因为它通过哈希函数将键直接映射为数组的索引,节省了存储空间。

            哈希表是一种非常重要且常用的数据结构,广泛应用于各种编程场景中,如数据库索引、编译器、缓存等领域。

            哈希表是一种用于存储键值对的数据结构。哈希表使用哈希函数来计算将在其中插入或搜索元素的数组的索引。这是一个用于实现哈希表的 C++ 程序。

    算法

    Begin
       Initialize the table size T_S to some integer value.
       Create a structure hashTableEntry to declare key k and value v.
       Create a class hashMapTable:
       Create a constructor hashMapTable to create the table.
       Create a hashFunc() function which return key mod T_S.
       Create a function Insert() to insert element at a key.
       Create a function SearchKey() to search element at a key.
       Create a function Remove() to remove element at a key.
       Call a destructor hashMapTable to destroy the objects created by the constructor.
       In main, perform switch operation and enter input as per choice.
       To insert key and values, call insert().
       To search element, call SearchKey().
       To remove element, call Remove().
    End.

    示例代码一:

    #include
    #include
    #include
    #include
    using namespace std;
    const int T_S = 200;
    
    class HashTableEntry {
       public:
          int k;
          int v;
          HashTableEntry(int k, int v) {
             this->k= k;
             this->v = v;
          }
    };
    
    class HashMapTable {
       private:
          HashTableEntry **t;
       public:
          HashMapTable() {
             t = new HashTableEntry * [T_S];
             for (int i = 0; i< T_S; i++) {
                t[i] = NULL;
             }
          }
          int HashFunc(int k) {
             return k % T_S;
          }
          void Insert(int k, int v) {
             int h = HashFunc(k);
             while (t[h] != NULL && t[h]->k != k) {
                h = HashFunc(h + 1);
             }
             if (t[h] != NULL)
                delete t[h];
             t[h] = new HashTableEntry(k, v);
          }
          int SearchKey(int k) {
             int h = HashFunc(k);
             while (t[h] != NULL && t[h]->k != k) {
                h = HashFunc(h + 1);
             }
             if (t[h] == NULL)
                return -1;
             else
                return t[h]->v;
          }
          void Remove(int k) {
             int h = HashFunc(k);
             while (t[h] != NULL) {
                if (t[h]->k == k)
                   break;
                h = HashFunc(h + 1);
             }
             if (t[h] == NULL) {
                cout<<"No Element found at key "<>c;
          switch(c) {
             case 1:
                cout<<"Enter element to be inserted: ";
                cin>>v;
                cout<<"Enter key at which element to be inserted: ";
                cin>>k;
                hash.Insert(k, v);
             break;
             case 2:
                cout<<"Enter key of the element to be searched: ";
                cin>>k;
                if (hash.SearchKey(k) == -1) {
                   cout<<"No element found at key "<>k;
                hash.Remove(k);
             break;
             case 4:
                exit(1);
             default:
                cout<<"\nEnter correct option\n";
          }
       }
       return 0;
    }

    输出

    1.Insert element into the table
    2.Search element from the key
    3.Delete element at a key
    4.Exit
    Enter your choice: 1
    Enter element to be inserted: 1
    Enter key at which element to be inserted: 1
    1.Insert element into the table
    2.Search element from the key
    3.Delete element at a key
    4.Exit
    Enter your choice: 1
    Enter element to be inserted: 2
    Enter key at which element to be inserted: 2
    1.Insert element into the table
    2.Search element from the key
    3.Delete element at a key
    4.Exit
    Enter your choice: 1
    Enter element to be inserted: 4
    Enter key at which element to be inserted: 5
    1.Insert element into the table
    2.Search element from the key
    3.Delete element at a key
    4.Exit
    Enter your choice: 1
    Enter element to be inserted: 7
    Enter key at which element to be inserted: 6
    1.Insert element into the table
    2.Search element from the key
    3.Delete element at a key
    4.Exit
    Enter your choice: 2
    Enter key of the element to be searched: 7
    No element found at key 7
    1.Insert element into the table
    2.Search element from the key
    3.Delete element at a key
    4.Exit
    Enter your choice: 2
    Enter key of the element to be searched: 6
    Element at key 6 : 7
    1.Insert element into the table
    2.Search element from the key
    3.Delete element at a key
    4.Exit
    Enter your choice: 3
    Enter key of the element to be deleted: 1
    Element Deleted
    1.Insert element into the table
    2.Search element from the key
    3.Delete element at a key
    4.Exit
    Enter your choice: 4 

    示例代码二: 

    在 C++ 中使用标准库中的 `unordered_map` 实现的哈希表示例代码:

    #include
    #include
    #include

    using namespace std;

    int main() {
        // 声明一个哈希表,键为 string 类型,值为 int 类型
        unordered_map hashTable;

        // 在哈希表中插入键值对
        hashTable["Alice"] = 25;
        hashTable["Bob"] = 30;
        hashTable["Charlie"] = 35;

        // 访问哈希表中的值
        cout << "Alice's age: " << hashTable["Alice"] << endl;

        // 修改哈希表中的值
        hashTable["Bob"] = 32;

        // 遍历哈希表
        for (const auto& pair : hashTable) {
            cout << pair.first << ": " << pair.second << endl;
        }

        // 检查特定键是否存在
        if (hashTable.find("Charlie") != hashTable.end()) {
            cout << "Charlie is in the hash table" << endl;
        }

        return 0;
    }
            在这个示例中,我们使用 `unordered_map` 类来实现哈希表,`unordered_map` 定义在 `` 头文件中,并提供了丰富的操作来处理键值对的插入、访问、修改和遍历等操作。

  • 相关阅读:
    shell 内置命令
    一道python难题5
    OAuth2:资源服务器
    使用Spring Boot整合定时任务(Schedule)
    vue3学习源码笔记(小白入门系列)------computed是如何工作的
    kafka-消费者组(SpringBoot整合Kafka)
    Linux用户管理— 用户相关文件-passwd文件- shadow文件-其他相关文件
    【状语从句练习题】although vs but
    spring cloud tencent:基于polaris手把手搭建微服务框架(附带源码)(二)
    1688API接入说明(商品详情数据示例)
  • 原文地址:https://blog.csdn.net/hefeng_aspnet/article/details/136203559