G
gibffe
Hello
I have a big problem with hash_map in stl.
I'm trying to create hash_map<const char *, char *> map
to keep key => val pairs as 4 byte C "strings".
Everything is ok when i do let's say
my_hash["abc"] = "efg";
my_hash["aa"] = "bb";
and then
for (it = my_hash.begin(); it != my_hash.end(); ++it)
printf("%s %s\n", it->first, it->second);
it will print 2 entries
The problem starts when i try to push values to my_hash that
i read from a file. Here's a bit of code:
for (i = 0; i < N2; i++)
{
fread_unlocked(mro1, sizeof(char), 4, stdin);
fread_unlocked(mro2, sizeof(char), 4, stdin);
m_hash[mro1] = (char *) malloc(4*sizeof(char));
memcpy(m_hash[mro1], mro2, 4);
/*
or like this:
m_hash[mro1] = mro2
*/
}
mro1 and mro2 are 4*sizeof(char) bytes malloced vectors
for (it = my_hash.begin(); it != my_hash.end(); ++it)
printf("%s %s\n", it->first, it->second);
will print only one, last entry i read from file. I figured out that it no
longer keeps "strings" as keys but pointers. Can someone tell me how to make
it work like my_hash["dljfj"] = "dlkfjkjdf"; ?
gibffe
I have a big problem with hash_map in stl.
I'm trying to create hash_map<const char *, char *> map
to keep key => val pairs as 4 byte C "strings".
Everything is ok when i do let's say
my_hash["abc"] = "efg";
my_hash["aa"] = "bb";
and then
for (it = my_hash.begin(); it != my_hash.end(); ++it)
printf("%s %s\n", it->first, it->second);
it will print 2 entries
The problem starts when i try to push values to my_hash that
i read from a file. Here's a bit of code:
for (i = 0; i < N2; i++)
{
fread_unlocked(mro1, sizeof(char), 4, stdin);
fread_unlocked(mro2, sizeof(char), 4, stdin);
m_hash[mro1] = (char *) malloc(4*sizeof(char));
memcpy(m_hash[mro1], mro2, 4);
/*
or like this:
m_hash[mro1] = mro2
*/
}
mro1 and mro2 are 4*sizeof(char) bytes malloced vectors
for (it = my_hash.begin(); it != my_hash.end(); ++it)
printf("%s %s\n", it->first, it->second);
will print only one, last entry i read from file. I figured out that it no
longer keeps "strings" as keys but pointers. Can someone tell me how to make
it work like my_hash["dljfj"] = "dlkfjkjdf"; ?
gibffe