std::hash in C++

Deeko

Lifer
Jun 16, 2000
30,213
11
81
Anyone here used the std::hash object in C++? I realize it isn't official STL yet, but it's common enough - its found its way into GCC at any rate.

Anyway, I'm trying to convert strings into unique INTs for identification purposes, and I figured a hash would do the job well enough. However, identical strings going into the hash function are producing inconsistent results, any idea why?

The code is very simple...

 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,328
4,004
75
I'm not a c++ expert, but if I had to guess, I'd say it was hashing the value of the pointer, rather than the value of the string the pointer points to.

Try changing the first character of a string, and see if the hash stays the same.
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Yep, you have a hash of char pointers not a hash of strings...

 

Deeko

Lifer
Jun 16, 2000
30,213
11
81
I've done it with every compilable combination of string, char, char* and const char*...always the same result.
 

Deeko

Lifer
Jun 16, 2000
30,213
11
81
Ok - I think you guys are right, but for the life of me I can't fix it. If I actually enter like below, it works - but not if its stored in a variable, no matter what I try.

 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Did you try dereferencing your pointer with &? Been awhile since I touched C++ though, so feel free to ignore me if I don't make any sense :p
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Deeko
Ok - I think you guys are right, but for the life of me I can't fix it. If I actually enter like below, it works - but not if its stored in a variable, no matter what I try.

Can you give an example of what doesn't work? I'm a little confused as to what you mean by "its stored in a variable". And could you be more specific about what constitutes success? How can you tell "its not working"?
 

Deeko

Lifer
Jun 16, 2000
30,213
11
81
Originally posted by: degibson
Originally posted by: Deeko
Ok - I think you guys are right, but for the life of me I can't fix it. If I actually enter like below, it works - but not if its stored in a variable, no matter what I try.

Can you give an example of what doesn't work? I'm a little confused as to what you mean by "its stored in a variable". And could you be more specific about what constitutes success? How can you tell "its not working"?

My assumption is that its doing what was suggested above - its hashing the pointer value, not the actual string contained in the variable. I know its "not working" because
A) two identical strings stores in different objects return different hash values that change every time I run it and
B) if I do my second example - manually hashing the value of the string (which obviously I can't do since it can change) - produces a different, but CONSISTENT result.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Deeko
My assumption is that its doing what was suggested above - its hashing the pointer value, not the actual string contained in the variable. I know its "not working" because
A) two identical strings stores in different objects return different hash values that change every time I run it and
Is this for std::hash<string> ?

E.g., with this, I would expect identical strings to hash to different values:
std::hash<const char*> idHash; ostringstream idStr; idStr << idHash(data.toString().c_str());


B) if I do my second example - manually hashing the value of the string (which obviously I can't do since it can change) - produces a different, but CONSISTENT result.

What happens if you do this:

string foo = "Foo";
std::hash<string> h;
h(foo);

?

Can you provide a pointer to your documentation on std::hash? Googling turns up nothing obvious, and my gcc apparently doesn't support it.
 
Sep 29, 2004
18,656
67
91
Please supply compiler versio n number
Is this cygwin under Windows or is it Unix/Linux?

Can you supply a sample program that compiles and runs? That will allow us geeks to try it out at home.


The funny thing is that I did some test code about 6 months ago testing out hashmaps in STL via Visual Studio. STL is not the proper acronym, it should be NSTL, the extra N for Non-Standardized Template Library.