modify has function from string parameter to templates...

This is a discussion on modify has function from string parameter to templates... within the C++ Programming forums, part of the General Programming Boards category; hello, I have this function: template <class T> unsigned int HashTable<T>::hash( const string & key ) const { unsigned int ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    9

    Question modify has function from string parameter to templates...

    hello, I have this function:

    template <class T> unsigned int HashTable<T>::hash( const string & key ) const
    {
    unsigned int hashVal = 0;
    // cout << key << "%";
    for( size_t i = 0; i < key.size(); i++ )
    hashVal = ( hashVal << 5 ) ^ key[ i ] ^ hashVal;
    return hashVal;
    }

    how to I convert it so it works with all data types?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    To format your code properly for the board, put a [/code] tag at the end of your source code, and a [code] at the beginning. Edit your post and add those (with indentation) - you're much more likely to get help if your source code is easier to read.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Posts
    5,453
    you can pass the hash function as a template parameter instead.

    Code:
     template <class T, class hash = some_sensible_default>
    class HashTable {
     //...
     };
    Code:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21