当前位置: 首页 > 工具软件 > libcstl > 使用案例 >

【libcstl】map用法。

徐高韵
2023-12-01

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;
}
 类似资料: