c语言中的hashmap

定义

c 复制代码
struct hashTable {
    int key;
    int val;
    UT_hash_handle hh;
};

struct hashTable* hashtable;

struct hashTable* find(int ikey) {
    struct hashTable* tmp;
    HASH_FIND_INT(hashtable, &ikey, tmp);
    return tmp;
}

void insert(int ikey, int ival) {
    struct hashTable* it = find(ikey);
    if (it == NULL) {
        struct hashTable* tmp = malloc(sizeof(struct hashTable));
        tmp->key = ikey, tmp->val = ival;
        HASH_ADD_INT(hashtable, key, tmp);
    } else {
        it->val = ival;
    }
}

使用

c 复制代码
hashtable = NULL;
struct hashTable* it = find(target - nums[i]);
insert(nums[i], i);
相关推荐
czy87874752 分钟前
用C语言实现原型模式
c语言·原型模式
czy87874754 分钟前
用C语言实现原型模式时,如何确定需要深拷贝还是浅拷贝?
c语言·原型模式
维诺菌32 分钟前
k8s java应用pod内存占用过高问题排查
java·jvm·云原生·容器·性能优化·kubernetes
5pace36 分钟前
【JavaWeb|第二篇】SpringBoot篇
java·spring boot·后端
oak隔壁找我37 分钟前
Spring AOP源码深度解析
java·后端
oak隔壁找我40 分钟前
MyBatis Plus 源码深度解析
java·后端
oak隔壁找我40 分钟前
Druid 数据库连接池源码详细解析
java·数据库·后端
oak隔壁找我42 分钟前
MyBatis 源码深度解析
java·后端
lang2015092843 分钟前
Spring 4.1新特性:深度优化与生态整合
java·后端·spring