The reason the code does not compile as is is that the hash<> template is fairly limited and will not apply to the std::string type. The following code includes a string hasher that fixes the compile problem.
Code:
#include <iostream>
#include <ext/hash_map>
#include <functional>
using namespace __gnu_cxx;
using namespace std;
struct hashstr
{
size_t operator()(const string& s) const
{
hash<const char*> H;
return H(s.c_str());
}
};
typedef hash_map< string, int, hashstr, equal_to<string> > map_type;
int main()
{
map_type m;
string kk = "mykey";
int vv = 2;
m[kk] = vv; // <= error here ...
return 0;
}