hashmap

Started by
13 comments, last by ArnoAtWork 19 years, 9 months ago
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.
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)...
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
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...
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
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....
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?


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

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.
Haaa, ok.
char* compare is done after int compare...it sounds normal in fact.
Thanks a lot.

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.

This topic is closed to new replies.

Advertisement