Jump to content
  • Advertisement
Sign in to follow this  
ArnoAtWork

hashmap

This topic is 5096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anybody has problem with hashmap of STLport with VC7.1? I don't known why, but if I do a loop using iterator from beginning to the end of the map, it never stops... Moreover, the find function always returns the end of the map even if the string is used. Thanks very much.

Share this post


Link to post
Share on other sites
Advertisement
VC.NET and STLPort uses different implementations of hashmap, they use different Predicates. STLPort wants a predicate that returns equal(a, b) while VC.NET wants a pred that returns less(a, b)...

Share this post


Link to post
Share on other sites
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};

typedef std::hash_map<char*, int, std::hash<const char*>, eqstr> StringToIntHashMap;

A note on this code: Don't use char*, use std::string instead...

Share this post


Link to post
Share on other sites
I don't know if it's the reason of the pb.

Why does an iterator set to the beginning of the map never reach the end(or do not valid the test [iterator == map.end()]) after a loop of ++ite? That never happens.

The only pb that i though was that the conversion string->integer was wrong. However, it seems correct....

Share this post


Link to post
Share on other sites
Huumm, I am confused.

In STL implementation, I have seen that a fonction already exists to convert string to integer. When you do for example: map[txt]=(int)
you can see that the txt is converted to integer, so after that, it's just a simple map implementation to search, access,....

So, how does the operator == can be right?


Share this post


Link to post
Share on other sites
Quote:
Original post by ArnoAtWork
Huumm, I am confused.

In STL implementation, I have seen that a fonction already exists to convert string to integer. When you do for example: map[txt]=(int)
you can see that the txt is converted to integer, so after that, it's just a simple map implementation to search, access,....

So, how does the operator == can be right?

Now I'm confused. But I'll try to explain what happens.
- First, the hash for the key (in your case the string) is calculated.
- Then, for every element that has the same hash, the key (not only its hash value) of the element is compared with the key you supplied. Only for an exact match, find returns the iterator for the element.

How the map is implemented in detail is up the the STL implementor.

At what point are you confused?

Share this post


Link to post
Share on other sites
For me a hash map converts the string to integer to store at the end a kind of map<int, something>.
So, I do not really understand why I have to give a char*==char* test because at the end, only integer representation of the text is used...

Share this post


Link to post
Share on other sites
But mapping from string to int may not be unique (the values that can be represented by an int is limeted, but not the number of different strings). Two different strings may produce the same hash, so you always have to compare the strings to make sure the key is really equal.

Share this post


Link to post
Share on other sites
Perhaps someone has a good link to the theory of hash maps for you, that could be of help. Can't find one in my bookmarks at the moment.
Perhaps this one.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!