实现哈希表 [英] Implement a hash table

查看:77
本文介绍了实现哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 C 中创建高效的查找表.

I'm trying to create an efficient look-up table in C.

我有一个整数作为键,而一个可变长度char*作为值.

I have an integer as a key and a variable length char* as the value.

我看过uthash,但这需要固定长度的char*值.如果我把这个数字大的话,那我就用了太多的内存.

I've looked at uthash, but this requires a fixed length char* value. If I make this a big number, then I'm using too much memory.

struct my_struct {
    int key;
    char value[10];             
    UT_hash_handle hh;
};

有人有指针吗?任何见解都将不胜感激.

Has anyone got any pointers? Any insight greatly appreciated.

感谢大家的回答.我已经使用uthash并定义了自己的自定义结构来容纳我的数据.

Thanks everyone for the answers. I've gone with uthash and defined my own custom struct to accommodate my data.

推荐答案

value字段声明为void *value.

这样,您可以将任何类型的数据作为值,但是分配和释放数据的责任将委托给客户端代码.

This way you can have any type of data as the value, but the responsibility for allocating and freeing it will be delegated to the client code.

这篇关于实现哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆