![](/img/trans.png)
[英]find the maximum value in a hash table and then printing all the variables with that value (in C)
[英]Hash table update value in c
我将Glib用于哈希表。 我需要从键更新值。 有没有一种方法,不删除并插入哈希表进行更新。
我发现了g_hash_table_replace ()
gboolean
g_hash_table_replace (GHashTable *hash_table,
gpointer key,
gpointer value);
是从密钥更新的值,如果这是我如何使用此功能。
解决:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <glib.h>
GHashTable * hash_operation = NULL;
int main(int argc, char *argv[]) {
char *from;
int gg = 3;
char *a=strdup("32"),*b=strdup("24"),*c=("mübarek");
hash_operation = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(hash_operation, a, gg);
from = strdup(g_hash_table_lookup(hash_operation, a));
printf("%s\n",from);
g_hash_table_replace (hash_operation, a,c);
from = strdup(g_hash_table_lookup(hash_operation, a));
printf("%s\n",from);
free(a);
free(b);
free(c);
free(from);
return 0;
}
问题解决了。
函数g_hash_table_replace
的用法非常简单:
它需要3个参数:
hash_table
:当然是您的哈希表,因此在您的情况下hash_operation
key
:您要编辑的密钥。 (我相信你的钥匙是a
) value
:应该存储在GHashTable中的key
一个简单的例子是:
GHashTable *table = g_hash_table_new(g_str_hash, g_str_equal);
gchar *key = "key1";
g_hash_table_insert(table, key, "Hello");
g_hash_table_replace(table, key, "World");
gchar *result = (gchar*) g_hash_table_lookup(table, key);
g_print("Result: %s\n", result); //Prints: "Result: World"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.