• [^] # Re: normal

    Posté par . En réponse au message Glib et les Binary Trees. Évalué à 3.

    Sache que t'es pas obligé de mettre des chaines de caractère dans un gtree,
    cependant voici un exemple pour que ton application fonctionne :

    #include <glib.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    GTree * redirected_connections;

    int main(int argc, char **argv)
    {
    redirected_connections = g_tree_new_full((GCompareDataFunc)strcmp,NULL,(GDestroyNotify)free,(GDestroyNotify)free);

    int i;
    for (i=1; i < 250; i++)
    {
    int value = (i * i);

    gchar* intkey = (gchar*)malloc(sizeof(gchar)*4);
    snprintf(intkey,4, "%d", i);

    gchar* intval = (gchar*)malloc(sizeof(gchar)*6);
    snprintf(intval,6, "%d", value);

    fprintf(stdout, "add %s:%s\n",intkey, intval );
    g_tree_insert(redirected_connections, intkey, intval);
    }

    for (i=1;i<250;i++)
    {
    gchar intkey[4];
    snprintf(intkey,4, "%d", i);

    fprintf(stdout, "check value %s:%s\n", intkey, g_tree_lookup(redirected_connections, intkey));
    }

    gint numberofnodes = g_tree_nnodes(redirected_connections);

    fprintf(stdout, "# of nodes : %d\n",numberofnodes);

    g_tree_destroy (redirected_connections);
    }