uthash哈希的使用
来源:互联网 发布:淘宝华夏陶瓷怎么样 编辑:程序博客网 时间:2024/05/29 14:35
做项目遇到需要使用到键值对的,一般我们会优先考虑map,multimap。
最近在看cocos2d-x源码,发现了一个更高效的用法uthash
头文件uthash.h在cocos2dx\support\data_support\下可以找到
下面是一个简单的使用:
#include "uthash.h"#include <stdio.h>#include <stdlib.h>/*这个uthash必须构造一个结构体*/struct packet{int key; /*这个是用来做hash的key值*/char msg[10]; UT_hash_handle hh; /*这个结构是uthash的结构体,里面包含next,prev,hash值等信息*/};int main(){struct packet *pkt, *tmp;int i;struct packet *hash_packet = NULL; /*必须初始化为NULL*//*打印这个hash的节点数*/printf ("hash count = %d \n", HASH_COUNT(hash_packet));/*往hash中添加节点*/for (i=0; i<10; i++){pkt = (struct packet *)malloc(sizeof(struct packet));pkt->key = i;sprintf (pkt->msg, "i=%d", i);HASH_FIND_INT(hash_packet, &i, tmp);if (tmp != NULL){printf ("The key(%d) exists in hash. \n", i);continue;}HASH_ADD_INT(hash_packet, key, pkt);printf ("insert item. key=%d,value=%p \n", i, pkt);}printf ("hash count = %d \n", HASH_COUNT(hash_packet));/*通过key查找*/for (i=0; i<13; i++){HASH_FIND_INT(hash_packet, &i, tmp);if (tmp == NULL){printf ("find not item. key=%d,value=%p \n", i, tmp);continue;}printf ("find item. key=%d,value=%p \n", i, tmp);}printf ("hash count = %d \n", HASH_COUNT(hash_packet));/*遍历这个hash表*/struct packet* mytemp = NULL;for (mytemp = hash_packet; mytemp != NULL; mytemp = (packet*)mytemp->hh.next)printf (" %d => %s \n", mytemp->key, mytemp->msg);/*删除节点*/for (i=0; i<13; i++){HASH_FIND_INT(hash_packet, &i, tmp);if (tmp == NULL){printf ("find not item. key=%d,value=%p \n", i, tmp);continue;}/*删除节点不会释放你的空间必须自己释放*/HASH_DEL(hash_packet, tmp);free(tmp);printf ("delete itme. key=%d,value=%p \n", i, tmp);}printf ("hash count = %d \n", HASH_COUNT(hash_packet));system("pause");return 0;}
2 0
- uthash哈希的使用
- 一个很好用的哈希 uthash
- data_support/uthash(使用哈希表的一些函数)
- 超好用的uthash万岁~~~
- uthash的三个数据结构
- UThash 的数据结构
- UThash 的数据结构
- 超好用的uthash万岁~~~
- uthash
- uthash
- uthash
- 简单好用的hash表-----uthash
- C开源hash代码uthash的用法总结(1)
- C开源hash代码uthash的用法总结(2)
- 开源hash代码uthash的原理与用法
- C开源hash代码uthash的用法总结(1)
- C开源hash代码uthash的用法总结
- 基于uthash的GNU/linux下简单hash_table模板类
- Android菜鸟的成长笔记(4)——你真的理解了吗?
- String类的equals和==方法
- 2. MariaDB激活二进制日志
- 回顾我的2013
- mysql 不用配置环境变量启动
- uthash哈希的使用
- 教你如何删除我的电脑中的百度云管家图标
- 项目管理之一——开发和测试
- C/C++获取文件大小(长度)的方法
- boost库简介
- Oracle TEMP表空间切换
- 点击A标签执行js或jquery脚本
- 篮球规则
- 许朝军血泪经验:点点网为什么失败了?