Hi,

I have difficulty declaring a hash_set. I would like a hash set where the key is a structure Point3d (typedef Point3D {short x,y,z}) and the value in the set associated with the key an int. I don't quite understand how to declare it ... something like
hash_set< const Point3D, ???, ??> mySet;
??

Any hints how I can do this ?

thanks alot !!

matt-
ps : since it is an extension of STL, is the usage somewhat standardized. I am working in MS Visual Studio

Template arguments are <class _Value, class _HashFcn, class _EqualKey, ... >
You probably only need to redifine _HashFcn thought...

struct newHashFnc
{
  size_t operator(const Point3D &p3d)
  {
    return p3d.x << 16 | p3d.y << 8 | p3d.z ; // or any other mixing you'd like
  }
} ;

...

(odd namespace)::hash_set< Point3D, newHashFnc > hs ;

thanks !! Actually, since I need a pair of key and a value (the Point3D, and a value associated with it) I thought I may be better using a hash_map instead of a hash_set, because it seems the hash key is the element of the set on its own, right ? Or am I wrong ?

thanks,

matt-

In the mean time, I made it a little further using a hash_map (still don't know if a hash set would be fine too), but now the compiler complains because it cannot convert const Point3D to size_t. I thought any type could be used as a key ? What's wrong ? Here's the code snippet :

hash_map < Point3D, int, hash_compare<Point3D,less<Point3D> >, std::allocator<pair<Point3D,int> > > skeletonPointSet;
hash_map <Point3D, int> :: const_iteratorskeletonPointSetIterator;

...

Point3D pt = {x,y,z};
skeletonPointSetIterator = skeletonPointSet.find(pt);

It is this last instruction taht the compiler doesn't like...

Any hints ?

thanks alot !!

matt-

Yes, hash_map is probably the correct thing to use in this case...

Hmm, one problem is you are using different types, that iterator is not neccecarily the same as the one returned (might work ofcourse if there is some implicit conversion)...

About key, you can use any type of value for key but you _must_ have a way of converting it to a well distributed integer type for indexing...

If you want to know why check out http://www.eternallyconfuzzled.com/brain.html (I think it is written by a member of this forum, good reading especially if you have not completed some algorithm introduction course).

My guess is that the default implementation tries to simply convert the Point3D to a size_t, which isnt implemented... so either you extend your hash_compare or try adding a operator size_t() function (I'm a little rusty on the syntax of these).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.