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

  • Home
  • SEARCH
  • 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 6005199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:22:00+00:00 2026-05-23T01:22:00+00:00

I’m writing a simple hash map class: template <class K, class V> class HashMap;

  • 0

I’m writing a simple hash map class:

template <class K, class V> class HashMap;

The implementation is very orthodox: I have a heap array which doubles in size when it grows large. The array holds small vectors of key/value pairs.

Vector<Pair<K, V> > *buckets;

I would like to overload the subscript operator in such a way that code like this will work:

HashMap<int, int> m;
m[0] = 10; m[0] = 20;
m[2] = m[1] = m[0];

In particular,

  • For m[k] = v where m does not contain k, I’d like a new entry to be added.
  • For m[k] = v where m does contain k, I’d like the old value to be replaced.
  • In both of these cases, I’d like the assignment to return v.

Presumably the code will look something like

V& operator[](K &key)
{
    if (contains(key))
    {
        // the easy case
        // return a reference to the associated value
    }
    else
    {
        Vector<Pair<K, V> > *buck = buckets + hash(k) % num_buckets;
        // unfinished
    }
}

How should I handle the case where the key is not found? I would prefer to avoid copying values to the heap if I can.

I suppose I could make a helper class which overloads both the assignment operator and a cast to V, but surely there is a simpler solution?

Edit: I didn’t realize that std::map required that the value type have a zero argument constructor. I guess I will just default-construct a value as well.

  • 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-23T01:22:01+00:00Added an answer on May 23, 2026 at 1:22 am

    It sounds like what you want is a “smart reference”, which you cannot generically implement in C++ because you cannot overload the dot operator (among other reasons).

    In other words, instead of returning a reference to a V, you would return a “smart reference” to a V, which would contain a pointer to V. That smart reference would implement operator=(const V &v) as this->p = new V(v), which only requires a copy constructor (not a zero-argument constructor).

    The problem is that the smart reference would have to behave like an actual reference in all other ways. I do not believe you can implement this in C++.

    One not-quite-solution is to have your constructor take a “default” instance of V to use for initializing new entries. And it could default to V().

    Like this:

    template<class K, class V> class HashMap {
    private:
        V default_val;
    public:
        HashMap(const V& def = V()) : default_val(def) {
            ...
        }
    ...
    };
    

    When V lacks a zero-argument constructor, HashMap h will not compile; the user will need to provide a V object whose value will be returned when a key is accessed for the first time.

    This assumes V has a copy constructor, of course. But from your examples, that seems like a requirement anyway.

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

Sidebar

Related Questions

No related questions found

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.