首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >uthash向<struct,struct> hashmap添加新条目

uthash向<struct,struct> hashmap添加新条目
EN

Stack Overflow用户
提问于 2019-11-07 16:13:23
回答 1查看 256关注 0票数 0

我想用乌塔什创建一个hashmap。

我希望键和值是一个结构,包含一个字符串和一个size_t,如下所示:

代码语言:javascript
复制
typedef struct hash_ptr {
    char* string;
    size_t len;
}hash_ptr;

哈希表本身如下所示:

代码语言:javascript
复制
typedef struct hash_map_entry {
    struct hash_ptr *key;
    struct hash_ptr *value;
    UT_hash_handle hh;
}hash_map_entry;

为了向地图中添加新条目,我编写了一个名为add_entry()的新函数:

代码语言:javascript
复制
void add_entry(hash_map_entry *map, hash_ptr *key, hash_ptr *value) {
    hash_map_entry *entry;
    HASH_FIND(hh, map, key, sizeof *key, entry);
    if (entry == NULL) {
        entry = (hash_map_entry*) malloc(sizeof *entry);
        memset(entry, 0, sizeof *entry);
        entry->value = value;
        entry->key = key;
        HASH_ADD(hh, map, key, sizeof *key, entry);
    }
}

但是,在初始化和调用add_entry()之后.

代码语言:javascript
复制
hash_map_entry *map = NULL;

hash_ptr *key = (hash_ptr*) malloc(sizeof *key);
memset(key, 0, sizeof *key);
key->string = "Is this the Krusty Krab?";
key->len = strlen(key->string);

hash_ptr *value = (hash_ptr*) malloc(sizeof *value);
memset(value, 0, sizeof *value);
value->string = "No, this is Patrick!";
value->len = strlen(value->string);

add_entry(map, key, value);

...HASH_FIND找不到添加的条目:

代码语言:javascript
复制
hash_map_entry *find_me;
HASH_FIND(hh, map, key, sizeof *key, find_me);

find_me为空。

我遵循了使用结构作为来自官方用户指南的键的说明。

我哪里错了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-07 19:03:53

这是我能想到的最简单的修改。与原文相比的变化如下:

  1. add_entry的第一个参数从hash_map_entry *map更改为hash_map_entry **map,并相应地调整代码。
  2. 使用HASH_ADD_KEYPTRstruct hash_ptr中散列字符串的内容,而不是散列struct hash_ptr本身。注意:代码假定len成员是string成员指向的对象的长度(以字节为单位)。
  3. 与2相关,将HASH_FIND的用法更改为散列struct hash_ptr中字符串的内容。

结果如下:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include "uthash.h"

typedef struct hash_ptr {
    char* string;
    size_t len;
}hash_ptr;

typedef struct hash_map_entry {
    struct hash_ptr *key;
    struct hash_ptr *value;
    UT_hash_handle hh;
}hash_map_entry;

void add_entry(hash_map_entry **map, hash_ptr *key, hash_ptr *value) {
    hash_map_entry *entry;
    HASH_FIND(hh, *map, key->string, key->len, entry);
    if (entry == NULL) {
        entry = (hash_map_entry*) malloc(sizeof *entry);
        memset(entry, 0, sizeof *entry);
        entry->value = value;
        entry->key = key;
        HASH_ADD_KEYPTR(hh, *map, key->string, key->len, entry);
    }
}

int main(void)
{
    hash_map_entry *map = NULL;

    hash_ptr *key = (hash_ptr*) malloc(sizeof *key);
    memset(key, 0, sizeof *key);
    key->string = "Is this the Krusty Krab?";
    key->len = strlen(key->string);

    hash_ptr *value = (hash_ptr*) malloc(sizeof *value);
    memset(value, 0, sizeof *value);
    value->string = "No, this is Patrick!";
    value->len = strlen(value->string);

    add_entry(&map, key, value);

    hash_map_entry *find_me;
    HASH_FIND(hh, map, key->string, key->len, find_me);
    if (find_me)
    {
        printf("found key=\"%s\", val=\"%s\"\n", find_me->key->string, find_me->value->string);
    }
    else
    {
        printf("not found\n");
    }
    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58752824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档