Jonathan Mcdougall wrote:
peter_k wrote: Hi
I've defined hash_map in my code using this:
-------------------------------------------
#include <string>
#include <hash_map.h>
&
namespace __gnu_cxx {
template<>
struct hash<std::string> {
hash<char*> h;
size_t operator()(const std::string &s) const {
return h(s.c_str());
};
};
};
&
hash_map<string, string, hash<string> > words;
hash_map is non standard. Please ask in a g++ newgroup next time.
Well, STL has a hash_map, and the std::tr1 has an unordered_map; and
the type of problem being reported is possible with those or any other
hashed container.
-------------------------------------------
I have no trouble with saving or reading data from this hash_map. But
when i'm doing something like...
hash_map<string, string, hash<string> >::iterator pointer;
pointer = words.begin();
cout << *pointer; // <-- error here :(
hash_map probably returns a std::pair, as std::map does. It makes
sense, an iterator points to an entry in the map and an entry is in
key=>value form.
Look in the doc for the allowed operations on a hash_map::iterator.
Very probably, you'll need to do
cout << pointer->first;
for the key and
cout << pointer->second;
for the value.
A hash (or unordered) map stores items by a "hash", that is, an integer
value calculated from the stored item's value. As long as hash values
for different stored items are likely to be unique, the container will
be able to retrieve any of its items very quickly. It follows
therefore, that a hash map needs a function (which is called the hash
function) to calculate a hash value from the value of an item. In this
case, a hash map of std::strings needs a hash function for a
std::string.
The error being reported is that the hash map cannot find a hash
function for std::string. To fix the problem the program should
therefore define one. The easiest way to do so is to re-use the hash
function for a character pointer (const char *). Here is an example of
how this might be done for gcc's hash_map:
#include <string>
template <>
struct gnu::hash<std::string>
{
size_t operator()( const std::string& s)
{
return hash<const char *>()( s.c_str() );
}
};
Note that I just put this declaration together and have not tested it.
Greg