Tutorial and example to SGI STL hash_set

P

Pierre Couderc

Generally, is there somewhere a good tutorial and examplefor the use of
SGI STL hash_set?
I am lost in SGI documentation.

More specifically, i am trying to use hat I need that a hash_set :

hash_set<c> h;

and logically the h function for my c class is missing and I get a
compile error.

How do I declare this hash function?
 
M

mlimber

Pierre said:
Generally, is there somewhere a good tutorial and examplefor the use of
SGI STL hash_set?

I don't know, but you can get a tutorial for the very similar and
nearly standardized std::tr1::unordered_set in Pete Becker's book on
TR1.
I am lost in SGI documentation.

More specifically, i am trying to use hat I need that a hash_set :

hash_set<c> h;

and logically the h function for my c class is missing and I get a
compile error.

How do I declare this hash function?

You need to specialize the hash functor for your class:

#include <hash_set>

class C { /*...*/ };

// Hashable classes must have an == operator
// We'll just stub it out here, pending definition of C
bool operator==( const C&, const C& ) { return true; }

namespace std
{
template<> struct hash<C>
{
// Define the hash function. We'll just stub it out here.
size_t operator()( const C& ) const { return 0; }
};
}

void hash_set_test()
{
C c;
std::hash_set<C> hsc;
hsc.insert( c );
}

Cheers! --M
 
P

pierre

Thank you,
It is exactly the example that was missing me. I am sure that it will
be useful tou those who will be looking for "hash-set tutorial"...
The exact syntax of the hash function in not evident for me, even it it
should...
Thank you again,
Pierre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top