I feel exhausted when trying to use the container unordered_map with char* as the key (on Windows, I am using VS 2010). I know that I have to defin
Using a char pointer as a key like you are above is almost certainly not what you want to do.
STL containers deal with stored values, in the case of std::unordered_map<char *, unsigned int, ...>, you are dealing with pointers to c strings, which may not even be around on subsequent insertion/removal checks.
Note that your my_unordered_map is a global variable but you are trying to insert local char arrays a, b, and c. What do you expect your comparison function my_equal_to() to strcmp() when the inserted c strings fall out of scope? (You suddenly have keys pointing to random garbage that can be compared to newly inserted future values.)
It is important that STL map keys be copyable values that cannot have their meanings changed by external program behavior. You should almost certainly use std::string or similar for your key values, even if their construction seems wasteful to you at first glance.
The following will work exactly as you intend things to work above, and is vastly safer:
#include <unordered_map>
#include <iostream>
#include <string>
using namespace std;
// STL containers use copy semantics, so don't use pointers for keys!!
typedef unordered_map<std::string, unsigned int> my_unordered_map;
my_unordered_map location_map;
int main() {
char a[10] = "ab";
location_map.insert(my_unordered_map::value_type(a, 10));
char b[10] = "abc";
location_map.insert(my_unordered_map::value_type(b, 20));
char c[10] = "abc";
location_map.insert(my_unordered_map::value_type(c, 20));
cout << "map size: " << location_map.size() << endl;
my_unordered_map::iterator it;
if ((it = location_map.find("abc")) != location_map.end()) {
cout << "found \"" << it->first << "\": " << it->second << endl;
}
return 0;
}
You comparator is fine (although passing a nullptr is undefined and probably should be handled)
The hash, ::std::tr1::hash<char*> is hashing off pointers so each "abc" goes (usually) in a different bucket
You need to write your own hash function that guarantees that hash("abc") always gives the same answer
For now - performance will be terrible, but have a hash that returns 0 - and you should see the second "abc" match the first
As per comments - using std::string simplifies memory management and provides a library supported hash and comparator, so just std::unordered_map<std::string, X> will work. This also means that upon deletion of the unordered map all strings will be deallocated for you. You can even instantiate the std::strings from char arrays on the stack safely.
If you still want to use char * then you will still need your own comparator and hash, but you can use std::shared_ptr to manage the memory for you (do not use stack instances - do a new char[])
you will then have a std::unordered_map<shared_ptr<char *>, X> but have no complications later from memory leaks.
If you still want to use char * you are on the right track, but it is important that you use a memory leak tool like purify or valgrind to make sure that you truly have all the memory management under control. (This is generally a good idea for any project)
Finally, global variables should be avoided.
When you define something such as "abc" it get assigned a const char*. Every time that you write "abc" within your program there is going to be a new memory alocated. So:
const char* x = "abc";
const char* y = "abc";
return x==y;
Will always return false because new memory is alocated each time "abc" is wrriten (sorry if I sound a bit repetitive).