Thread: modify has function from string parameter to templates...

  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
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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
    Location
    Waterloo, Texas
    Posts
    5,708
    you can pass the hash function as a template parameter instead.

    Code:
     template <class T, class hash = some_sensible_default>
    class HashTable {
     //...
     };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 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, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01: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