hashing a string to an int question

This is a discussion on hashing a string to an int question within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I am trying to make a hashing function that will accept a string and output an integer. On ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    hashing a string to an int question

    Hi guys,

    I am trying to make a hashing function that will accept a string and output an integer. On this forum I found this code (made by Salem) which adds up the ASCII values of the letters.

    Code:
    char string[] = "Bob Jones";
    
    int hash ( const char *string ) {
      int result = 0;
      while ( *string ) result += *string++;
      return result;
    }

    It works fine for using character array stings, however, I would prefer to use a string declared as..

    Code:
    string name = "Bob Jones";
    Is there anyway to accomplish the same thing using strings?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    hash(name.str())?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    So the string is a class that has a member function named str?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    name.c_str() instead
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Quote Originally Posted by Mario F.
    name.c_str() instead
    Ah! Is the general structure of the string class posted anywhere? I would like to familiarize myself with it's member functions/data members (I'm still kind of new to this).

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    http://www.cppreference.com/ is an excellent quick resource. And downloadable too!

    EDIT: Although not always complete.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >On this forum I found this code (made by Salem) which adds up the ASCII values of the letters.
    I remember that. I also seem to remember that he mentioned how awful it is. Not only is the algorithm poor for hashing, it's also dangerous because it's so very easy to overflow an int. Here's a better one:
    Code:
    unsigned hash ( const char *key )
    {
      unsigned h = 0;
    
      for ( int i = 0; key[i] != '\0'; i++ )
        h = 33 * h + key[i];
    
      return h;
    }
    >Is there anyway to accomplish the same thing using strings?
    Code:
    int h = hash ( name.c_str() );
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Sweet! Just what I needed. Thanks Mario F.!

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    Smile

    Quote Originally Posted by Prelude
    >On this forum I found this code (made by Salem) which adds up the ASCII values of the letters.
    I remember that. I also seem to remember that he mentioned how awful it is. Not only is the algorithm poor for hashing, it's also dangerous because it's so very easy to overflow an int. Here's a better one:
    Code:
    unsigned hash ( const char *key )
    {
      unsigned h = 0;
    
      for ( int i = 0; key[i] != '\0'; i++ )
        h = 33 * h + key[i];
    
      return h;
    }
    >Is there anyway to accomplish the same thing using strings?
    Code:
    int h = hash ( name.c_str() );
    Thanks Prelude,

    I did modify the code from it's original form. I simply posted it like that because it was simpler. Thank you for the suggestions, they've given me some new ideas.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Thank you for the suggestions, they've given me some new ideas.
    Just a note, hashing is something you really want to avoid experimenting on in production code. It's really hard to come up with a good algorithm and really easy to come up with a truly awful algorithm.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 06:26 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM

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