Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Tutorial and example to SGI STL hash_set

Reply
Thread Tools

Tutorial and example to SGI STL hash_set

 
 
Pierre Couderc
Guest
Posts: n/a
 
      12-28-2006
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?
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      12-28-2006
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<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

 
Reply With Quote
 
 
 
 
pierre@couderc.cc
Guest
Posts: n/a
 
      12-30-2006

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" <(E-Mail Removed)> wrote:
> 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<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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash_set was not declared even I #include<hash_set> eric C++ 3 07-12-2011 10:27 AM
newbie Q: how to use stl hash_set in gcc3.4.4 zl2k C++ 2 05-14-2006 07:30 PM
STL hash_set problem, SGI impl Timo Qvist C++ 1 11-18-2004 03:04 PM
How to Install SGI STL into MS Visual C++ v.6.0 JZapin C++ 2 04-30-2004 03:05 PM
SGI STL Chaman Singh C++ 1 04-12-2004 11:11 PM



Advertisments