5.完整程序例子

5.1.key类型为int的完整的例子

#include <stdio.h>   /* gets */
#include <stdlib.h>  /* atoi, malloc */
#include <string.h>  /* strcpy */
#include "uthash.h"struct my_struct {int ikey;                    /* key */char value[10];UT_hash_handle hh;         /* makes this structure hashable */
};static struct my_struct *g_users = NULL;void add_user(int mykey, char *value) {struct my_struct *s;HASH_FIND_INT(users, &mykey, s);  /* mykey already in the hash? */if (s==NULL) {s = (struct my_struct*)malloc(sizeof(struct my_struct));s->ikey = mykey;HASH_ADD_INT( users, ikey, s );  /* ikey: name of key field */}strcpy(s->value, value);
}struct my_struct *find_user(int mykey) {struct my_struct *s;HASH_FIND_INT( users, &mykey, s );  /* s: output pointer */return s;
}void delete_user(struct my_struct *user) {HASH_DEL( users, user);  /* user: pointer to deletee */free(user);
}void delete_all() {struct my_struct *current_user, *tmp;HASH_ITER(hh, users, current_user, tmp) {HASH_DEL(users,current_user);  /* delete it (users advances to next) */free(current_user);            /* free it */}
}void print_users() {struct my_struct *s;for(s=users; s != NULL; s=(struct my_struct*)(s->hh.next)) {printf("user ikey %d: value %s\n", s->ikey, s->value);}
}int name_sort(struct my_struct *a, struct my_struct *b) {return strcmp(a->value,b->value);
}int id_sort(struct my_struct *a, struct my_struct *b) {return (a->ikey - b->ikey);
}void sort_by_name() {HASH_SORT(users, name_sort);
}void sort_by_id() {HASH_SORT(users, id_sort);
}int main(int argc, char *argv[]) {char in[10];int ikey=1, running=1;struct my_struct *s;unsigned num_users;while (running) {printf(" 1. add user\n");printf(" 2. add/rename user by id\n");printf(" 3. find user\n");printf(" 4. delete user\n");printf(" 5. delete all users\n");printf(" 6. sort items by name\n");printf(" 7. sort items by id\n");printf(" 8. print users\n");printf(" 9. count users\n");printf("10. quit\n");gets(in);switch(atoi(in)) {case 1:printf("name?\n");add_user(ikey++, gets(in));break;case 2:printf("id?\n");gets(in); ikey = atoi(in);printf("name?\n");add_user(ikey, gets(in));break;case 3:printf("id?\n");s = find_user(atoi(gets(in)));printf("user: %s\n", s ? s->value : "unknown");break;case 4:printf("id?\n");s = find_user(atoi(gets(in)));if (s) delete_user(s);else printf("id unknown\n");break;case 5:delete_all();break;case 6:sort_by_name();break;case 7:sort_by_id();break;case 8:print_users();break;case 9:num_users=HASH_COUNT(users);printf("there are %u users\n", num_users);break;case 10:running=0;break;}}delete_all();  /* free any structures */return 0;
}

5.2.key类型为字符数组的完整的例子

#include <string.h>  /* strcpy */
#include <stdlib.h>  /* malloc */
#include <stdio.h>   /* printf */
#include "uthash.h"struct my_struct {char name[10];             /* key (string is WITHIN the structure) */int id;UT_hash_handle hh;         /* makes this structure hashable */
};int main(int argc, char *argv[]) {const char **n, *names[] = { "joe", "bob", "betty", NULL };struct my_struct *s, *tmp, *users = NULL;int i=0;for (n = names; *n != NULL; n++) {s = (struct my_struct*)malloc(sizeof(struct my_struct));strncpy(s->name, *n,10);s->id = i++;HASH_ADD_STR( users, name, s );}HASH_FIND_STR( users, "betty", s);if (s) printf("betty's id is %d\n", s->id);/* free the hash table contents */HASH_ITER(hh, users, s, tmp) {HASH_DEL(users, s);free(s);}return 0;
}

5.3.key类型为字符指针的完整的例子

#include <string.h>  /* strcpy */
#include <stdlib.h>  /* malloc */
#include <stdio.h>   /* printf */
#include "uthash.h"struct my_struct {const char *name;          /* key */int id;UT_hash_handle hh;         /* makes this structure hashable */
};int main(int argc, char *argv[]) {const char **n, *names[] = { "joe", "bob", "betty", NULL };struct my_struct *s, *tmp, *users = NULL;int i=0;for (n = names; *n != NULL; n++) {s = (struct my_struct*)malloc(sizeof(struct my_struct));s->name = *n;s->id = i++;HASH_ADD_KEYPTR( hh, users, s->name, strlen(s->name), s );}HASH_FIND_STR( users, "betty", s);if (s) printf("betty's id is %d\n", s->id);/* free the hash table contents */HASH_ITER(hh, users, s, tmp) {HASH_DEL(users, s);free(s);}return 0;
}

C开源hash代码uthash的用法总结(2)相关推荐

  1. C开源hash代码uthash的用法总结(1)

    uthash 是C的比较优秀的开源代码,它实现了常见的hash操作函数,例如查找.插入.删除等待.该套开源代码采用宏的方式实现hash函数的相关功能,支持C语言的任意数据结构最为key值,甚至可以采用 ...

  2. C中哈希开源hash代码uthash的原理与用法

    开源hash代码uthash的原理与用法 说明 说明 需要包含头文件 #include "uthash.h" 参考leedcode1 添加链接描述 struct hashTable ...

  3. c开源hash项目 uthash的用法总结

    uthash 是C的比较优秀的开源代码,它实现了常见的hash操作函数,例如查找.插入.删除等待.该套开源代码采用宏的方式实现hash函数的相关功能,支持C语言的任意数据结构最为key值,甚至可以采用 ...

  4. 如何学习Python开源项目代码

    2019独角兽企业重金招聘Python工程师标准>>> 阅读Python开源项目代码主要有如下三个原因: 1. 在工作过程中遇到一些问题,Google和StackOverFlow等网 ...

  5. 免费开源低代码拖拽开发_资料来源:面向开源开发人员的免费代码搜索工具

    免费开源低代码拖拽开发 开源代码的金矿可供程序员使用,但是选择正确的库并了解如何使用它可能很棘手. Sourcegraph创建了一个搜索引擎和代码浏览器,以帮助开发人员找到更好的代码并更快地构建软件. ...

  6. 如何读懂python代码_教你如何阅读 Python 开源项目代码

    作者: Destiny 来源:https://learnku.com/articles/23010/teach-you-to-read-the-python-open-source-project-c ...

  7. 【radar】毫米波雷达相关开源项目代码汇总(工具箱、仿真、2D毫米波检测、融合、4D毫米波检测、分割、SLAM、跟踪)(6)

    [radar]毫米波雷达相关开源项目代码汇总(工具箱.仿真.2D毫米波检测.融合.4D毫米波检测.分割.SLAM.跟踪)(6) Toolbox pymmw https://github.com/m6c ...

  8. 「SAP技术」SAP MM 事务代码ME17的用法

    SAP MM 事务代码ME17的用法 1, 如下采购信息记录需要被归档: PIR号码,5300007816 2, ME12打上删除标记, 3, 事务代码ME17做归档 3.1 创建archive fi ...

  9. Kd-Tree算法原理和开源实现代码

    Kd-Tree算法原理和开源实现代码 本文介绍一种用于高维空间中的快速最近邻和近似最近邻查找技术--Kd-Tree(Kd树).Kd-Tree,即K-dimensional tree,是一种高维索引树形 ...

最新文章

  1. Tether销毁5亿USDT;BCH将于11月15日硬分叉,SV-Pool已向普通矿工开放
  2. 面试:MySQL InnoDB 事务隔离
  3. pil模块python_python PIL模块的基本使用
  4. vue中$watch源码阅读笔记
  5. TrueCrypt 为何决定终止项目
  6. InfluxDB-Python 操作实践
  7. cent os mysql 内存_Cent OS – MySQL – 主从配置
  8. 批处理命令——for
  9. vue 使用了浏览器的刷新之后报错_98、解决vue-cli3项目运行时sockjs报错问题
  10. python snownlp_snownlp · PyPI
  11. 【MOOC测试】数学模型
  12. [bzoj1001]狼抓兔子 最小割
  13. 镇楼篇--转行初入IT的心路历程
  14. 高效数字音频功率放大器NTP8928
  15. linux虚拟磁盘服务,hintsoft Linux iscsi虚拟磁盘完全教程及优化攻略
  16. go语言教程哪里有?go 语言优秀开源项目汇总
  17. 基于微信小程序的设备报修系统源码
  18. 企业公众号推送什么内容?企业公众号内容可以发哪些?
  19. vim中指定字符串的替换和删除
  20. 蚂蚁开放平台开发第三方授权登陆(一):开发前期准备

热门文章

  1. docker hub push_如何制作Docker镜像(image)?
  2. 【数据库基础知识】数据库表格——主键和外键
  3. 问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
  4. spring boot 配置网关时404错误_网关Spring Cloud Gateway科普
  5. mac永久更换php默认版本,mac如何修改默认php
  6. android用java写文本框_Android 使用TextView实现验证码输入框
  7. php 类加载其它类,PHP 命名空间 namespace / 类别名 use / 框架自动载入 机理的
  8. linux sublime 命令行启动,命令行 – 使用命令行在Sublime Text 3中打开一个文件夹
  9. mysql 存储过程 模糊查询_mysql 分页创建存储过程并实现模糊查询
  10. python db2 linux 安装,python安装DB2模块