C有哈希/字典数据结构吗?

ola*_*ala 4 c hashtable

我正在学习C现在来自知道perl和一点python.我做了一个快速搜索,发现在perl/python中没有显式的哈希/字典,我看到人们说你需要一个函数来查找哈希表.所以事实是C不提供固有的哈希结构,你必须编写一些函数才能在C中使用哈希?

Osw*_*ald 8

基本上,C所拥有的唯一数据结构是数组,结构(有点像地图,但必须在编译时知道密钥)和联合.其他所有内容必须手动编码或由图书馆提供.


小智 6

忽略上面所有误导性的答案,C 确实有一个标准库,早在所有这些奇特的语言存在之前。基本版本只做一张表,GNU 版本可以做任意数量的表。

#include <search.h>

int hcreate(size_t nel);

ENTRY *hsearch(ENTRY item, ACTION action);

void hdestroy(void);

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <search.h>

int hcreate_r(size_t nel, struct hsearch_data *htab);

int hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab);

void hdestroy_r(struct hsearch_data *htab);
Run Code Online (Sandbox Code Playgroud)

结帐:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/hcreate.html