【libcstl】map用法。

1.结构体

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cstl/cmap.h>    

typedef struct
{
	char msg[16];
}MyData_ST; 

int main()
{
	map_t* pt_map = NULL;
    pair_t* pt_pair = NULL;
    map_iterator_t it_iter;
	MyData_ST stMsg;
	int iIdx = 0;
	
	//结构体类型注册
	type_register(MyData_ST, NULL, NULL, NULL, NULL);
	//创建
	pt_map = create_map(int, MyData_ST);
    pt_pair = create_pair(int, MyData_ST);
	//初始化	
    pair_init(pt_pair);
    map_init_ex(pt_map, NULL);
	//插入
	iIdx = 66;
	memset(&stMsg, 0x00, sizeof(stMsg));
	strcpy((char *)&stMsg, "hello~");
    pair_make(pt_pair, iIdx, &stMsg);
    it_iter = map_insert(pt_map, pt_pair);
	
	iIdx = 88;
	memset(&stMsg, 0x00, sizeof(stMsg));
	strcpy((char *)&stMsg, "word~");
    pair_make(pt_pair, iIdx, &stMsg);
    it_iter = map_insert(pt_map, pt_pair);
	//查找
	iIdx = 66;
	it_iter = map_find(pt_map, iIdx);
	printf("[003]:%d\n", (*((int *)pair_first((pair_t*)iterator_get_pointer(it_iter)))));
	printf("[004]:%s\n", ((MyData_ST *)pair_second((pair_t*)iterator_get_pointer(it_iter)))->msg);

	//销毁
    map_destroy(pt_map);
    pair_destroy(pt_pair);
	
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值