Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 855591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:06:00+00:00 2026-05-15T08:06:00+00:00

I have to create a lookup table for C function names (as keys) to

  • 0

I have to create a lookup table for C function names (as keys) to function pointers (as values). I am thinking of using STL’s hash_map container, as the access time of a hash table is O(1). Is there any good hash function for this? Currently I am using (31*H + c) as my hash function.

Also, does STL’s hash_map takes care of collisions, or I have to take care of them in my code? Please give some examples if possible.

Sample Code I am currently working upon

#include <iostream>
#include <ext/hash_map>;

using namespace std;
using namespace __gnu_cxx;

namespace __gnu_cxx {
#ifndef __HASH_STRING__
#define __HASH_STRING__
  template <>
    struct hash<string> {
      size_t operator() (const std::string& s) const
      {
        size_t h = 0;
        std::string::const_iterator p, p_end;
        for(p = s.begin(), p_end = s.end(); p != p_end; ++p)
        {
          h = 31 * h + (*p);
        }
        return h;
      }
    };
#endif
};

int main()
{
  hash_map<string, int> months;

  months["january"] = 1;
  months["february"] = 2;
  months["march"] = 3;
  months["april"] = 4;
  months["may"] = 5;
  months["june"] = 6;
  months["july"] = 7;
  months["august"] = 8;
  months["september"] = 9;
  months["october"] = 10;
  months["november"] = 11;
  months["december"] = 12;

  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:06:01+00:00Added an answer on May 15, 2026 at 8:06 am

    Assuming you’ve got the full STL, it actually includes a hash function, hash<T>, which in its included form is suitable for a few different key types including char* (C strings). I don’t know details of its performance, but the STL is generally engineered to have acceptable performance for most applications.

    As for collisions, that’s for hash_map to deal with, you needn’t worry about it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a lookup table in OCaml. The table will have
I have create a vcf file that contains contacts by using this code ContentResolver
Description I'm trying to JOIN a table using the result of a stored function.
For example I have a tag lookup table joining tags to 3 different types
I have two tables: A lookup table ( tabOne ): KEY | Group |
Let's say I have a type of lookup table which I can build for
I have a table with symbol names (e.g. functions) and their start memory address
Guys i currently have 2 tables that reference a lookup table. Ive been tasked
I have a case where I need to translate (lookup) several values from the
I'm trying to create a lookup table of member functions in my code, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.