【问题标题】:Hashmap Print Function Not Updating SetsHashmap 打印功能不更新集合
【发布时间】:2016-09-21 16:45:52
【问题描述】:

我的 hashmap 打印功能似乎有问题。它正在打印正确的值,但不正确的键。在下面的代码中,我得到了两个键/值对的用户输入,它只打印出第二个输入的键。

例如,第一对输入 12/"e",第二对输入 15/"f",输出为 f | 12、f| 15. 有人知道发生了什么吗?我认为这可能与 char 数组有关,我会使用字符串,但我只能使用原语来完成这项任务。 100 的 char 数组的大小只是一个任意大的数字,我认为用户密钥不会超过这个数字。谢谢!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;

typedef struct _node
{
    char *key;
    int value;          /* For this, value will be of type int */
    struct _node *next; /* pointer to the next node in the list */
} node;

/* HashMap class */
class HashMap
{
private:
    node ** hashTable;
    int numSlots;
public:
    /* Initializes a hashmap given its set size */
    HashMap(int size)
    {
        numSlots = size;
        hashTable = new node*[size] ;
        for (int i = 0; i < size; i++)
        {
            hashTable[i] = NULL;
        }
    }

    /*** Hash function. ***/

    int hash(char *s)
    {
        int i;
        int sum = 0;

        for (i = 0; * (s + i) != '\0'; i++)
        {
            sum += *(s + i);
        }

        return (sum % numSlots);
    }

    /* Create a single node. */
    node *create_node(char *key, int value)
    {
        node *result = new node();
        result->key = key;
        result->value = value;
        result->next = NULL;

        return result;
    }

    /*
     *Stores given key/value pair in hashmap
     *returns boolean for success/failure
     */

    void set (char* key, int value)
    {
        int keyValue = hash(key);
        node *current = hashTable[keyValue];
        node *original = current;
        node *newNode;
        if (current == NULL)
        {
            hashTable[keyValue] = create_node(key, value);
        }
        else
        {
            while (current != NULL)
            {
                current = current -> next;
            }

            if (current == NULL)
            {
                newNode = create_node(key, value);
                newNode -> next = original;
                hashTable[keyValue] = newNode;
            }
        }
    }

    /* Prints hash table */

    void print_hash_table()
    {
        int i;
        node *listIterator = NULL;

        for (i = 0 ; i < numSlots ; i++)
        {
            listIterator = hashTable[i];

            if (listIterator != NULL)
            {
                cout << listIterator->key << " | ";
                while (listIterator != NULL)
                {
                    cout << listIterator->value << " ";
                    listIterator = listIterator -> next;
                }
                cout << endl;
            }


        }

    }
};


int main()
{
    HashMap hash (128);
    char key[100];
    int value;

    cout << "Enter element to be inserted: ";
    cin >> value;
    cout << "Enter key at which element to be inserted: ";
    cin >> key;
    hash.set(key, value);
    cout << "Enter element to be inserted: ";
    cin >> value;
    cout << "Enter key at which element to be inserted: ";
    cin >> key;
    hash.set(key, value);
    hash.print_hash_table();
    return 0;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!听起来您可能需要学习如何使用调试器来逐步执行代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。延伸阅读:How to debug small programs
  • 这段代码很难调试......代码显示奇怪的行为......查看code.geeksforgeeks.org/pPUDRe

标签: c++ hashmap char


【解决方案1】:

您将一个字符指针传递给set,并将该指针存储在您的地图中。然后您更改key 中的内容,这将更改您已存储的第一个键的值,并将相同的指针再次存储在您的地图中。所以这两个键将具有相同的值。

如果它们被删除或调用函数返回,这也可能导致映射中的指针指向无效的内存区域,从而使传入的数组超出范围。

当您将密钥存储在哈希映射中时,您需要对其进行复制。

【讨论】:

    猜你喜欢
    • 2018-07-10
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多