sizeof he sizeof()
时间: 2023-11-06 22:52:25 浏览: 39
sizeof是一个C语言的运算符,用于获取数据类型或变量的大小(以字节为单位)。它可以用于各种数据类型,如基本数据类型、数组、结构体等。
当使用sizeof运算符时,可以通过两种方式使用:
1. sizeof(数据类型):用于获取特定数据类型的大小。例如,sizeof(int)将返回int类型所占用的字节数。
2. sizeof(变量名):用于获取变量的大小。例如,sizeof(num)将返回变量num所占用的字节数。
而对于sizeof(),在括号内不传入任何参数时,它会返回一个表达式或类型的大小。例如,sizeof(3+4)将返回整数表达式3+4的大小。
相关问题
用c写一个hashe map
我可以为你编写一段示例代码,以供参考:```
#include <stdio.h>
#include <stdlib.h>// 定义一个hash map结构
typedef struct hashmap {
int key;
int value;
struct hashmap *next;
} hashmap;// 初始化一个hash map
hashmap *hashmap_init(){
hashmap *map = (hashmap *)malloc(sizeof(hashmap));
if(map == NULL) {
return NULL;
}
map->key = 0;
map->value = 0;
map->next = NULL;
return map;
}// 向hash map中添加新的键值对
int hashmap_put(hashmap *map, int key, int value) {
hashmap *tmp = map;
while(tmp->next != NULL){
tmp = tmp->next;
}
hashmap *new_node = (hashmap *)malloc(sizeof(hashmap));
new_node->key = key;
new_node->value = value;
new_node->next = NULL;
tmp->next = new_node;
return 0;
}// 从hash map中获取特定key的value
int hashmap_get(hashmap *map, int key) {
hashmap *tmp = map;
while(tmp != NULL){
if(tmp->key == key){
return tmp->value;
}
tmp = tmp->next;
}
return -1;
}int main(){
hashmap *map = hashmap_init();
hashmap_put(map, 1, 10);
hashmap_put(map, 2, 20);
hashmap_put(map, 3, 30); int value = hashmap_get(map, 2);
printf("value of key 2 is %d\n", value);
return 0;
}
```
#include <stdio.h> #include <string.h> #define Tablesize 1000005 int zzt[Tablesize],count; void insert(char *ID, int distance); struct Record { char ID[19]; int distance; int he; }Record; struct Record records[Tablesize]; int hash(char *ID); int find(char *ID); int main() { char ID[19]; for (int i = 0; i < sizeof(zzt) / sizeof(zzt[0]); i++) { zzt[i] = -1;} int n, k,m,distance; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%s %d", ID, &distance); if (distance < k) distance = k; insert(ID, distance); } scanf("%d", &m); int t = -1; for (int i = 0; i < m; i++) { scanf("%s", ID); t = find(ID); if (t == -1) printf("No Info\n"); else printf("%d\n", records[t].distance); } } int find(char *ID) { int h = hash(ID); for (int i = zzt[h]; i != -1; i = records[i].he) if (!strcmp(records[i].ID, ID)) return i; return -1; } int hash(char *ID) { long int yy = 0; for (int i = 0; i < 17; i++) yy = 10 * yy + (ID[i] - '0'); return yy%Tablesize; } void insert(char *ID, int distance) { int o = hash(ID); for (int i = zzt[o]; i != -1; i = records[i].he) { if (!strcmp(records[i].ID, ID)) { records[i].distance= records[i].distance+distance; return 0; } } records[count].distance = records[count].distance+distance; strncpy(records[count].ID, ID, 18); records[count].he = zzt[o]; zzt[o] = count++; }对每一个指令添加注释
// 定义哈希表的大小
#define Tablesize 1000005
// 定义哈希表数组和计数器
int zzt[Tablesize], count;
// 插入函数,用于将 ID 和 distance 插入哈希表中
void insert(char *ID, int distance);
// 定义记录结构体
struct Record {
char ID[19];
int distance;
int he;
}Record;
// 定义记录结构体数组
struct Record records[Tablesize];
// 哈希函数,用于将 ID 转化为哈希表中的下标
int hash(char *ID);
// 查找函数,用于在哈希表中查找 ID 并返回其在记录结构体数组中的下标
int find(char *ID);
// 主函数
int main() {
// 定义变量
char ID[19];
int n, k, m, distance;
// 初始化哈希表数组
for (int i = 0; i < sizeof(zzt) / sizeof(zzt[0]); i++) {
zzt[i] = -1;
}
// 输入 n 和 k,分别代表 ID 的数量和最小距离
scanf("%d %d", &n, &k);
// 循环读入 ID 和 distance,并调用 insert 函数将其插入哈希表中
for (int i = 0; i < n; i++) {
scanf("%s %d", ID, &distance);
if (distance < k) distance = k;
insert(ID, distance);
}
// 输入 m,代表要查询的 ID 数量
scanf("%d", &m);
// 循环读入要查询的 ID,并调用 find 函数在哈希表中查找其在记录结构体数组中的下标
int t = -1;
for (int i = 0; i < m; i++) {
scanf("%s", ID);
t = find(ID);
if (t == -1) printf("No Info\n");
else printf("%d\n", records[t].distance);
}
}
// 查找函数,用于在哈希表中查找 ID 并返回其在记录结构体数组中的下标
int find(char *ID) {
int h = hash(ID);
for (int i = zzt[h]; i != -1; i = records[i].he)
if (!strcmp(records[i].ID, ID)) return i;
return -1;
}
// 哈希函数,用于将 ID 转化为哈希表中的下标
int hash(char *ID) {
long int yy = 0;
for (int i = 0; i < 17; i++)
yy = 10 * yy + (ID[i] - '0');
return yy%Tablesize;
}
// 插入函数,用于将 ID 和 distance 插入哈希表中
void insert(char *ID, int distance) {
int o = hash(ID);
for (int i = zzt[o]; i != -1; i = records[i].he) {
if (!strcmp(records[i].ID, ID)) {
records[i].distance= records[i].distance+distance;
return 0;
}
}
records[count].distance = records[count].distance+distance;
strncpy(records[count].ID, ID, 18);
records[count].he = zzt[o];
zzt[o] = count++;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)