A
Amit Gupta
I am puzzled, how the hash_map works. In the following declaration, I
expect it to hash on the character-string, but apparently it hash on
(maybe) the pointer value of string?
Any help is greatly appreciated.
#include <stdio.h>
#include <map>
#include <ext/hash_map>
class A {};
int main()
{
A* a1 = new A();
A* a2 = new A();
__gnu_cxx::hash_map<char*, A*> mapper;
mapper["jan"] = a1;
mapper["feb"] = a2;
char* copy = strdup("jan");
printf("%d\n", mapper[copy]);
// the following works correctly, as in give the right a1 pointer
// printf("%d\n", "jan");
printf("%d\n", mapper["feb"]);
return (0);
}
expect it to hash on the character-string, but apparently it hash on
(maybe) the pointer value of string?
Any help is greatly appreciated.
#include <stdio.h>
#include <map>
#include <ext/hash_map>
class A {};
int main()
{
A* a1 = new A();
A* a2 = new A();
__gnu_cxx::hash_map<char*, A*> mapper;
mapper["jan"] = a1;
mapper["feb"] = a2;
char* copy = strdup("jan");
printf("%d\n", mapper[copy]);
// the following works correctly, as in give the right a1 pointer
// printf("%d\n", "jan");
printf("%d\n", mapper["feb"]);
return (0);
}