Correctly using google sparse hash maps in MS VC++?

Started by
19 comments, last by Foofles 14 years ago
I seem to be having some issues in my program... I'm probably doing something wrong. Right now I'll declare the hash map as thus:

google::sparse_hash_map<const char*, unsigned long, stdext::hash_compare<const char*>> MyHash;




My goal here is to be able to retrieve/store unsigned long values by using a (const char *) key For example

MyHash["Pizza"] = 42;




etc. I attempt to see if a value is in the hash like so

if(!MyHash["Pizza"]){
//Pizza is not in the table! 
}else{
//Pizza is in the table!
}




Where am I going wrong? sometimes the Hash lookup messes up strings. Most of the time actually. As a for instance, I'd keep trying to see if Pineapple was in the table but it keeps returning Pizza. Any advice would be very appreciated :) [Edited by - Foofles on March 18, 2010 5:27:21 PM]
Advertisement
What does stdext::hash_compare<const char*> do?

Does it hash the pointer or the text?

Try hash_compare<std::string> instead.
Thanks for the reply :)

Hmmm that didn't occur to me before. I did try std::string instead but I'm still having the same problem.

This may sound silly but I'm not even sure what stdext::hash_compare does... it's a templated class buried in templated classes. So I'm not sure what ultimately happens when it gets to the const char * or std::string.

Is there a way to use my own compare function?
Quote:Original post by Foofles
Thanks for the reply :)

Hmmm that didn't occur to me before. I did try std::string instead but I'm still having the same problem.

This may sound silly but I'm not even sure what stdext::hash_compare does... it's a templated class buried in templated classes. So I'm not sure what ultimately happens when it gets to the const char * or std::string.

Is there a way to use my own compare function?


Probably. I don't know what stdext is (or what compiler/stdlib you're using). You'll have to look it up in your library's documentation.

Anything I tell you is going to come via Google.
Well thanks for the reply anyhow :)

I'm still stumped :( ... hash_compare defaults to a "less than" comparison function, I tried plugging in my own like:

struct eqstr{  bool operator()(const char* s1, const char* s2) const  {    return strcmp(s1, s2) == 0;  }};google::sparse_hash_map<const char*, unsigned long, stdext::hash_compare<const char*, eqstr>> MyHash;


But it still doesn't work. Maybe I'm incorrectly accessing whether a key is in the map or not. Anybody have any experience with google:sparse_hash_map ? or maybe even stdext::hash_map? This is giving me a world of trouble. :/
Try!

FWIW, I've had good experiences with boost's unordered_map.
Quote:Original post by the_edd
Try!

FWIW, I've had good experiences with boost's unordered_map.


Same here. You can also use the standard unordered_map with any compiler that has tr1 (Visual Studio 2008, for example). I believe Boost's version uses the standard one if it exists, and otherwise it's own implementation (which is similar).
I've been staring at that documentation page for days, tried that page's eqstr function and everything else on it as well. Nothing seems to work!

But this unordered_map is actually news to me. I will look into it right now. Thanks so much :)

Edit: OOps. Here I was so confident I had solved the problem. I haven't. Any advice on hash_map , google::sparse_hash_map or unordered_map is still greatly appreciated. :)

[Edited by - Foofles on March 18, 2010 5:01:41 PM]
Post the actual code you're using.
Quote:Original post by Foofles
I've been staring at that documentation page for days, tried that page's eqstr function and everything else on it as well. Nothing seems to work!


What do you mean by "work"? Have you copy-and-pasted the example on that page in to a text editor and got it to compile? Was there any part of *that specific example* that you didn't understand or your compiler wouldn't accept? Perhaps the part about tr1/ext::has/__gnu_cxx::hash was confusing? Which compiler are you using, anyway?

Concrete details are needed.

Quote:Edit: OOps. Here I was so confident I had solved the problem. I haven't. Any advice on hash_map , google::sparse_hash_map or unordered_map is still greatly appreciated. :)


Again, Google is a wonderful thing. Enter "boost" and "unordered_map". Take it from there.

This topic is closed to new replies.

Advertisement