• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

std::hash in C++

Deeko

Lifer
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...

 
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.
 
I've done it with every compilable combination of string, char, char* and const char*...always the same result.
 
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.

 
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 😛
 
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"?
 
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.
 
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.
 
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.
 
Back
Top