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 7494001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:16:37+00:00 2026-05-29T17:16:37+00:00

I’m learning about hash tables, trying to understand how they work. I’d like to

  • 0

I’m learning about hash tables, trying to understand how they work. I’d like to make a rather simple hash table with separate chaining (using lists in an array). I have a few questions:

  1. Assuming the keys can be of any type, I would require the user to implement the hashing function, right? Can this be avoided?

  2. The user also needs to supply the length of the array that contains the lists (for collisions) at initialisation, correct? Can this be avoided?

If you have any other tips, or maybe some clear code samples in C++ of a hash table I would be thankful.

Thanks for your help.

  • 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-29T17:16:38+00:00Added an answer on May 29, 2026 at 5:16 pm

    Typically, yes, you would need the client to specify the hash function, since if you are writing a generic hash table and are operating on an arbitrary type T, you can’t know how to hash it in a way that is semantically meaningful. You can do this by parameterizing the class on both the type of the elements being stored and the hash function. For example:

    template <typename T, typename Hash = std::hash<T>>
        class MyHashTable {
        /* ... */
    }
    

    Here, you can use default arguments with the templates to select the default hash function unless the user specifies otherwise.

    While the client typically can specify the initial size of the table, it’s not required. You could make an educated guess about the number of buckets (say, initially use 17 buckets), growing the table as the load factor increases. This is similar, say, to how std::vector works: the implementation can pick a default size, but if the client either explicitly asks for a presized vector or calls reserve, the implementation takes the hint from the user. For example, you could have a constructor of the form

    template <typename T, typename Hash = std::hash<T>>
        class MyHashTable {
    public:
        MyHashTable(unsigned numBuckets = 17);
    }
    

    This way, the client can just construct a hash table with a default number of buckets, or if they have a sense of the number of buckets they’d like they can specify it as a parameter. However, you might also want to hide the buckets as a detail and just have the client specify how many elements they’re expecting to put into the table, then have your class do a computation behind the scenes for this. This makes it easier to switch implementations behind-the-scenes, so that if you want to use something like a dynamic perfect hash table instead of chained hashing the class can handle the complexity of computing an initial size.

    As for code examples, I’m not sure how to provide any without giving away a lot of the complexity involved in building the hash table. 🙂 If there’s a specific piece of code you’re interested in and are having trouble writing on your own, feel free to post it as a separate question so that you can get more targeted feedback.

    Hope this helps!

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.