写一个用结构体当 key 的 map 的 c++的例子
时间: 2023-09-13 20:05:26 浏览: 176
### 回答1:
可以这样写:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point key;
int value;
} MapNode;
typedef struct {
MapNode *nodes;
int size;
int capacity;
} Map;
void map_init(Map *map) {
map->size = ;
map->capacity = 16;
map->nodes = (MapNode *) malloc(sizeof(MapNode) * map->capacity);
}
void map_put(Map *map, Point key, int value) {
if (map->size == map->capacity) {
map->capacity *= 2;
map->nodes = (MapNode *) realloc(map->nodes, sizeof(MapNode) * map->capacity);
}
MapNode *node = &map->nodes[map->size++];
node->key = key;
node->value = value;
}
bool map_get(Map *map, Point key, int *value) {
for (int i = ; i < map->size; i++) {
if (map->nodes[i].key.x == key.x && map->nodes[i].key.y == key.y) {
*value = map->nodes[i].value;
return true;
}
}
return false;
}
int main() {
Map map;
map_init(&map);
Point key1 = {1, 2};
Point key2 = {3, 4};
map_put(&map, key1, 10);
map_put(&map, key2, 20);
int value;
if (map_get(&map, key1, &value)) {
printf("(%d, %d) -> %d\n", key1.x, key1.y, value);
}
if (map_get(&map, key2, &value)) {
printf("(%d, %d) -> %d\n", key2.x, key2.y, value);
}
return ;
}
### 回答2:
下面是一个使用结构体作为键的C语言map示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义一个结构体作为map的key
typedef struct {
int id;
char name[50];
} MapKey;
// 定义一个结构体作为map的value
typedef struct {
int score;
} MapValue;
// 定义一个键值对结构
typedef struct {
MapKey key;
MapValue value;
} KeyValuePair;
// 定义map结构
typedef struct {
KeyValuePair* data;
int capacity; // map容量
int size; // map大小
} Map;
// 初始化map
void map_init(Map* map, int capacity) {
map->data = malloc(capacity * sizeof(KeyValuePair));
map->capacity = capacity;
map->size = 0;
}
// 添加键值对到map
void map_put(Map* map, MapKey key, MapValue value) {
if (map->size == map->capacity) {
// 扩容操作
map->capacity *= 2;
map->data = realloc(map->data, map->capacity * sizeof(KeyValuePair));
}
KeyValuePair pair;
pair.key = key;
pair.value = value;
map->data[map->size++] = pair;
}
// 根据key从map中获取value
MapValue* map_get(Map* map, MapKey key) {
for (int i = 0; i < map->size; i++) {
if (map->data[i].key.id == key.id && strcmp(map->data[i].key.name, key.name) == 0) {
return &map->data[i].value;
}
}
return NULL; // key不存在返回NULL
}
int main() {
Map map;
map_init(&map, 10);
MapKey key1 = {1, "Tom"};
MapValue value1 = {80};
map_put(&map, key1, value1);
MapKey key2 = {2, "Alice"};
MapValue value2 = {90};
map_put(&map, key2, value2);
MapKey key3 = {3, "Bob"};
MapValue value3 = {75};
map_put(&map, key3, value3);
MapKey searchKey = {2, "Alice"};
MapValue* result = map_get(&map, searchKey);
if (result != NULL) {
printf("Score of key '2, Alice' is %d\n", result->score);
} else {
printf("Key not found.\n");
}
free(map.data);
return 0;
}
```
这个示例定义了用于存储键值对的结构体`KeyValuePair`,以及用于作为map的结构体`Map`。初始化map时,需要指定容量,并通过`map_init`来分配内存。使用`map_put`向map中添加键值对,键值对的key和value通过结构体进行传递。通过`map_get`可以根据key获取对应的value。
在示例中,我们先创建一个map,并添加了三个键值对,然后使用`map_get`根据key查找其中一个value。最后释放内存。
阅读全文