c语言如何创建关联数组,如何在C中通过hcreate/hsearch(通过值而不是引用)实现关联数组的键?...

通过POSIX hcreate/hsearch功能implented使用关联数组(如描述here,我挣扎着,我从来没有进入一些意外的行为寻找钥匙或周围的其他方法。 我跟踪它到商店按引用的一些实例-instead-的价值 这令我感到诧异,因为在该示例使用字符串文字作为关键字:如何在C中通过hcreate/hsearch(通过值而不是引用)实现关联数组的键?

store("red", 0xff0000);

store("orange", 0x123456); /* Insert wrong value! */

store("green", 0x008000);

store("blue", 0x0000ff);

store("white", 0xffffff);

store("black", 0x000000);

store("orange", 0xffa500); /* Replace with correct value. */

这里是一个MWE,显示我的问题:

#include /* intptr_t */

#include /* hcreate(), hsearch() */

#include /* perror() */

#include /* exit() */

#include /* strcpy() */

void exit_with_error(const char* error_message){

perror(error_message);

exit(EXIT_FAILURE);

}

int fetch(const char* key, intptr_t* value){

ENTRY e,*p;

e.key=(char*)key;

p=hsearch(e, FIND);

if(!p) return 0;

*value=(intptr_t)p->data;

return 1;

}

void store(const char *key, intptr_t value){

ENTRY e,*p;

e.key=(char*)key;

p = hsearch(e, ENTER);

if(!p) exit_with_error("hash full");

p->data = (void *)value;

}

void main(){

char a[4]="foo";

char b[4]="bar";

char c[4]="";

intptr_t x=NULL;

if(!hcreate(50)) exit_with_error("no hash");

store(a,1); /* a --> 1 */

strcpy(c,a); /* remember a */

strcpy(a,b); /* set a to b */

store(a,-1); /* b --> -1 */

strcpy(a,c); /* reset a */

if(fetch(a,&x)&&x==1) puts("a is here.");

if(!fetch(b,&x)) puts("b is not.");

strcpy(a,b); printf("But if we adjust a to match b");

if(fetch(a,&x)&&x==-1&&fetch(b,&x)&&x==-1) puts(", we find both.");

exit(EXIT_SUCCESS);

}

Compili Ng和上述C代码产生以下输出执行:

a is here.

b is not.

But if we adjust a to match b, we find both.

我将需要读取文件,并存储了一大批串:整数对,然后我需要阅读的第二文件,以检查偶数以前存储的值的字符串数量较多。

我不明白如果通过引用来比较键,这将如何实现。

如何更改我的关联数组实现以按值存储键?

如果这是不可能的,那么考虑到上述用例,我该如何解决这个问题?

编辑:

这个问题只涉及与输入密钥,但没有找到。 也会出现相反的问题,并在this question中详细介绍。

2016-01-12

mschilli

+0

[C hsearch找到之前未输入的值]的可能重复(http://stackoverflow.com/questions/34752934/c-hsearch-finds-values-not-entered-before) –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
glibc 的 `hash_find` 函数用于在哈希表查找指定对应的。它的函数原型如下: ```c void *hash_find (struct hash_table *table, const void *key) ``` 其,`table` 表示要查找的哈希表,`key` 是要查找的。 下面是一个 `hash_find` 函数的例子: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <search.h> #define TABLESIZE 10 int main() { struct node { char *name; int age; } *person; struct node persons[] = { {"Alice", 18}, {"Bob", 20}, {"Charlie", 22}, {"David", 24}, {"Emma", 26}, {"Frank", 28}, {"Grace", 30}, {"Henry", 32}, {"Ivy", 34}, {"John", 36} }; struct hash_table *table; table = (struct hash_table *) calloc(1, sizeof(struct hash_table)); if (table == NULL) { perror("calloc"); exit(EXIT_FAILURE); } table->size = TABLESIZE; table->hash = string_hash; table->compare = strcmp; hcreate(TABLESIZE); int i; for (i = 0; i < sizeof(persons) / sizeof(persons[0]); i++) { person = &persons[i]; struct node **result = (struct node **) hsearch(table->entry, FIND, (void *) person->name, strlen(person->name)); if (result == NULL) { perror("hsearch"); exit(EXIT_FAILURE); } else if (*result != NULL) { printf("%s already in table with age %d\n", (*result)->name, (*result)->age); } else { *result = person; printf("%s added to table with age %d\n", person->name, person->age); } } struct node **result = (struct node **) hsearch(table->entry, FIND, "Charlie", strlen("Charlie")); if (result == NULL) { perror("hsearch"); exit(EXIT_FAILURE); } else if (*result == NULL) { printf("Charlie not found in table\n"); } else { printf("Charlie found in table with age %d\n", (*result)->age); } hdestroy(); return 0; } ``` 以上代码演示了如何使用 `hash_find` 函数在哈希表查找指定对应的。首先定义了一个 `node` 结构体表示一个人,包含了姓名和年龄两个属性。然后定义了一个 `persons` 数组,其存储了一些人的信息。接着创建了一个 `hash_table` 结构体,并调用 `hcreate` 函数创建了一个哈希表。然后遍历 `persons` 数组,逐个将人的信息加入到哈希表。在加入之前,先调用 `hsearch` 函数查找是否已经存在相同姓名的人。如果已经存在,就输出其年龄;否则,就将其加入到哈希表。最后再次调用 `hsearch` 函数查找指定姓名的人的年龄,并输出其年龄。最后调用 `hdestroy` 函数销毁哈希表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值