STL hash_map

Discussion in 'C++' started by Axel Gallus, Jul 27, 2007.

  1. Axel Gallus

    Axel Gallus Guest

    Hello,

    i have a question concerning STL non-standard hash_maps under Visual Studio
    2005:

    Microsoft STL requires a "hash_compare" object for hash_maps:

    template <class Key, class Type, class Traits=hash_compare<Key, less<Key> >,
    class Allocator=allocator<pair <const Key, Type> > >
    class hash_map

    I derived a own class from hash_compare dealing with unsigned integers and
    implemented the application operator() and a compare function:

    class Timestamp_Hashing : public stdext::hash_compare <unsigned int>

    {


    public:

    /// <summary>

    /// Class constructor. The constructor takes two arguments, one defining the
    quality of the hash map,

    /// the other to adapt to a stream's dts struct.</summary>


    Timestamp_Hashing (unsigned int HFactor, unsigned int FTimestamp = 0);

    /// <summary>

    /// This hash-function is used automatically by "hash_map" for determining
    in which bucket to put a certain timestamp.</summary>

    /// <param name="Timestamp"> The Timestamp for which the hash-value is to be
    caclulated. </param>

    /// <return> Returns a hash-value in form of the size_t type which can be
    used by iterators.</return>

    SIZE_T operator () (const unsigned int & Timestamp) const;



    /// <summary>

    /// This function is used automatically by "hash_map" for sorting the
    timestamps contained in the buckets. </summary>

    /// It receives two timestamps and return true if the first one is less the
    the second one.

    /// <param name="Timestamp1"> First timestamp.</param>

    /// <param name="Timestamp1"> Second timestamp.</param>

    /// <return> Returns true if Timestamp1 < Timestamp2 </return>


    bool operator () (const unsigned int & Timestamp1, const unsigned int &
    Timestamp2) const;




    private:

    // private variables used to save state during hash_map creation

    unsigned int Hash_Factor;

    unsigned int First_Timestamp;


    };



    As you can see my derived class Timestamp_Hashing has a constructor taking
    two arguments.

    I have planned to initiate a certain state of an object of class
    Timestamp_Hashing through this constructor which influences the
    hash_function.

    If i create a hash_map by

    stdext::hash_map < unsigned int, unsigned int, Timestamp_Hashing>;

    everything compiles well but if i use

    Timestamp_Hashing Hashy(10,20);
    stdext::hash_map < unsigned int, unsigned int, Hashy>;

    the compiler generated an error: Error 11 error C2923: 'stdext::hash_map' :
    'Hashy' is not a valid template type argument for parameter '_Tr'.



    So, how is it possible to pass a hash_compare/Timestamp_Hashing object with
    a certain state to the hash_map constructor - is there a workaround?



    Greetings and thx in advance.

    Axel
     
    Axel Gallus, Jul 27, 2007
    #1
    1. Advertising

  2. Axel Gallus wrote:
    > i have a question concerning STL non-standard hash_maps under Visual
    > Studio 2005:
    >
    > Microsoft STL requires a "hash_compare" object for hash_maps:
    >
    > template <class Key, class Type, class Traits=hash_compare<Key,
    > less<Key> >, class Allocator=allocator<pair <const Key, Type> > >
    > class hash_map
    >
    > I derived a own class from hash_compare dealing with unsigned
    > integers and implemented the application operator() and a compare
    > function:
    > class Timestamp_Hashing : public stdext::hash_compare <unsigned int>
    >
    > {
    > [..]
    > };
    >
    >
    >
    > As you can see my derived class Timestamp_Hashing has a constructor
    > taking two arguments.
    >
    > I have planned to initiate a certain state of an object of class
    > Timestamp_Hashing through this constructor which influences the
    > hash_function.
    >
    > If i create a hash_map by
    >
    > stdext::hash_map < unsigned int, unsigned int, Timestamp_Hashing>;
    >
    > everything compiles well but if i use
    >
    > Timestamp_Hashing Hashy(10,20);
    > stdext::hash_map < unsigned int, unsigned int, Hashy>;


    What is that line supposed to designate? It's not legal to write
    a simple type in a statement, without declaring an object or function
    with that type.

    Besides, you can't pass a non-type as a type template argument.
    "Hashy" is an object. The 'hash_map' template expects a *type* as
    its third argument.

    > the compiler generated an error: Error 11 error C2923:
    > 'stdext::hash_map' : 'Hashy' is not a valid template type argument
    > for parameter '_Tr'.
    >
    >
    > So, how is it possible to pass a hash_compare/Timestamp_Hashing
    > object with a certain state to the hash_map constructor - is there a
    > workaround?


    Yes, most likely. Pass it to the _constructor_ when defining your
    hash_map<...> _instance_:

    stdext::hash_map<..., Timestamp_Hashing> myMap(Hashy);

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 27, 2007
    #2
    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. Murali

    STL hash_map hash

    Murali, Feb 8, 2004, in forum: C++
    Replies:
    3
    Views:
    12,079
    John Harrison
    Feb 9, 2004
  2. Arun Dharankar
    Replies:
    3
    Views:
    8,685
    Arun Dharankar
    Jun 3, 2004
  3. gibffe

    [STL] hash_map problem

    gibffe, Oct 29, 2004, in forum: C++
    Replies:
    2
    Views:
    2,815
    John Harrison
    Oct 30, 2004
  4. Replies:
    1
    Views:
    702
    mlimber
    Jun 2, 2006
  5. STL hash_map

    , Dec 4, 2006, in forum: C++
    Replies:
    1
    Views:
    538
Loading...

Share This Page