2016-10-04 12 views
0

です。ハッシュテーブルには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; 
} 

問題は解決しました。

+0

まさにあなたの問題は何ですか?同様に、なぜその機能を使用できないのですか(試したコードを表示してください)?あるいは、その機能が不適切なのはなぜですか? – hyde

答えて

1

機能g_hash_table_replaceの使い方は非常に簡単です:

  • hash_table

    それは3つの引数を取るそれはあなたがあなたのケースhash_operation

  • keyにして、ハッシュテーブルはもちろんである:キーあなたは編集したいです。
  • value(私は信じてあなたの鍵はaです):key

でGHashTableに格納されなければならない値は簡単な例は次のようになります。

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" 
+0

ああ、私はあなたの質問に「解決」という言葉を忘れてしまった;)とにかく... – mame98

関連する問題