468,457 Members | 1,581 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Tutorial and example to SGI STL hash_set

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<ch;

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

How do I declare this hash function?
Dec 28 '06 #1
2 7355
Pierre Couderc wrote:
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<ch;

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<Chsc;
hsc.insert( c );
}

Cheers! --M

Dec 28 '06 #2

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

On Dec 28, 4:26 pm, "mlimber" <mlim...@gmail.comwrote:
Pierre Couderc wrote:
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<ch;
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<Chsc;
hsc.insert( c );
}

Cheers! --M
Dec 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Michael Foord | last post: by
1 post views Thread by Abhijit Ray | last post: by
5 posts views Thread by Bart Blommerde | last post: by
3 posts views Thread by Markus Dehmann | last post: by
31 posts views Thread by Antoon Pardon | last post: by
5 posts views Thread by Markus Dehmann | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.