Tutorial and example to SGI STL hash_set

Discussion in 'C++' started by Pierre Couderc, Dec 28, 2006.

  1. 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?
    Pierre Couderc, Dec 28, 2006
    #1
    1. Advertising

  2. Pierre Couderc

    mlimber Guest

    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
    mlimber, Dec 28, 2006
    #2
    1. Advertising

  3. Pierre Couderc

    Guest

    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" <> 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
    , Dec 30, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chaman Singh

    SGI STL

    Chaman Singh, Apr 12, 2004, in forum: C++
    Replies:
    1
    Views:
    515
    Christopher Benson-Manica
    Apr 13, 2004
  2. JZapin
    Replies:
    2
    Views:
    469
    Jerry Coffin
    Apr 30, 2004
  3. Timo Qvist

    STL hash_set problem, SGI impl

    Timo Qvist, Nov 18, 2004, in forum: C++
    Replies:
    1
    Views:
    866
    Victor Bazarov
    Nov 18, 2004
  4. zl2k
    Replies:
    2
    Views:
    560
  5. eric
    Replies:
    3
    Views:
    1,302
    Michael DOUBEZ
    Jul 12, 2011
Loading...

Share This Page