Re: how to have user defined hash for unordered_map ?
On Jul 11, 8:17 pm, James Kanze <james.ka...@gmail.com> wrote:
On Jul 11, 7:05 am, abir <abirba...@gmail.com> wrote:
I want a user defined key for tr1 unordered_map.
Pete Becker has given the reply to your question, but...
Previously i used unordered_map from boost, which automatically takes
hash_value friend to compute hash, so i thought it is in the tr1
standard.
Now this works for tr1,
unordered_map<WORKER,int,boost::hash<WORKER>> m;
but for boost, this even works.
unordered_map<WORKER,int> m;
with,
friend std::size_t hash_value(const self_type& self){
return self.pos() + self.w().count;
}
I am still not sure why i cant just have a overloaded hash or some
other function, and unordered_map automatically finds it like boost.
Why i have to specify hash function for my class while i don't need
for int, float ot string!
There was some problem of const correctness in previous post, actually
pos() & w() have to be const member function.
template<typename W>
class worker{
public:
typedef worker<W> self_type;
worker(W& w,int pos) : w_(&w),pos_(pos){}
bool operator== (const self_type& other) const {
assert(w_ == other.w_);
Do you mean this? If so, the simplest way of ensuring it is to
make w_ static.
return pos_ == other.pos_;
}
Yes i mean this.
w_ can't be static, as two worker can do different work.
Two worker doing different work are not comparable and so are not
allowed to form hash key for same container.
private:
W* w_;
int pos_;
friend std::size_t hash(const self_type& self){
return self.pos_ + w_->count;
}
};
--
James Kanze (GABI Software) email:james.ka...@gma=
il.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Dat=
enverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
Thanks
abir