views:

62

answers:

1

I would like to put use a string* as a key in an unordered_list. I do not want the hash the pointer itself but the string it points to.

I understand I need to create a struct like this:

struct myhash{
    size_t operator()(const string * str){
        return hash(*str);
    }
}

and send it as a a hasher to the map template, but i am not sure how.

+2  A: 

That's basically it. You then provide it as the third template parameter to the unordered_map type (Which I will assume to be the C++0x one). I would generalize it so it's usable in any situation, rather than just string:

struct dereference_hash
{
    template <typename T>
    std::size_t operator()(const T* pX)
    {
        return std::hash<T>()(*pX);
    }
};

typedef std::unordered_map<std::string*, int, dereference_hash> map_type;
GMan
Thanks, it works.A minor correction: return std::hash<T>(*pX);
izex
@izex: Oops, you're right I'm wrong, but do you mean `std::hash<T>()(*pX)`?
GMan
of course I do :-)
izex