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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:57:32+00:00 2026-06-13T12:57:32+00:00

i have to basically edit a quadratic probing file to work as a hash_map

  • 0

i have to basically edit a quadratic probing file to work as a hash_map library but i’m having trouble overloading the subscript operator so i can modify that specefic index.

the code i have so far is:

template <typename HashedObj, typename storedObj >
class HashTable
{
public:
    int& operator [] (const int nIndex)
    {
        return array[nIndex];//THIS IS WHATS WRONG
    }

    //create hashtable with siz of 101
    explicit HashTable(int size = 101) : array( nextPrime( size ) )
    { makeEmpty( ); }

    //return true if current index is active
    bool contains( const HashedObj & x ) const
    {
        return isActive( findPos( x ) );
    }

    //initiallize index as empty
    void makeEmpty( )
    {
        currentSize = 0;
        for( int i = 0; i < array.size( ); i++ )
            array[ i ].info = EMPTY;
    }

    //insert object into hash index and mark index as active
    bool insert( const HashedObj & x )
    {
        // Insert x as active
        int currentPos = findPos( x );
        if( isActive( currentPos ) )
            return false;

        array[ currentPos ] = HashEntry( x, ACTIVE );

        if( ++currentSize > array.size( ) / 2 )
            rehash( );

        return true;
    }

    //search for obj and mark index as deleted
    bool remove( const HashedObj & x )
    {
        int currentPos = findPos( x );
        if( !isActive( currentPos ) )
            return false;

        array[ currentPos ].info = DELETED;
        return true;
    }

    //declare three different entry types
    enum EntryType { ACTIVE, EMPTY, DELETED };

private:
    //each hash index stores the following
    struct HashEntry
    {
        //THIS WILL BE STRINGS OR INTS
        HashedObj element;
        //THIS WILL BE VECTOR STRINGS
        storedObj ilement; 
        //index status is stored
        EntryType info;
        //each entry is made of hashed obj and stored obj and the status is empty
        HashEntry( const HashedObj & e = HashedObj( ), const storedObj & f = storedObj( ),EntryType i = EMPTY )
        : element( e ), ilement( f ),info( i ) { }
    };

    //create an array of hashentries
    vector<HashEntry> array;
    //currentsize of the hash table is stored here
    int currentSize;

    bool isActive( int currentPos ) const
    { return array[ currentPos ].info == ACTIVE; }

    int findPos( const HashedObj & x ) const
    {
        int offset = 1;
        int currentPos = myhash( x );

        while( array[ currentPos ].info != EMPTY &&
              array[ currentPos ].element != x )
        {
            currentPos += offset;  // Compute ith probe
            offset += 2;
            if( currentPos >= array.size( ) )
                currentPos -= array.size( );
        }

        return currentPos;
    }

    void rehash( )
    {
        vector<HashEntry> oldArray = array;

        // Create new double-sized, empty table
        array.resize( nextPrime( 2 * oldArray.size( ) ) );
        for( int j = 0; j < array.size( ); j++ )
            array[ j ].info = EMPTY;

        // Copy table over
        currentSize = 0;
        for( int i = 0; i < oldArray.size( ); i++ )
            if( oldArray[ i ].info == ACTIVE )
                insert( oldArray[ i ].element );
    }
    int myhash( const HashedObj & x ) const
    {
        int hashVal = hashing( x );

        hashVal %= array.size( );
        if( hashVal < 0 )
            hashVal += array.size( );

        return hashVal;
    }
};

int hashing( const string & key );
int hashing( int key );

the point is for main code to be able to do something like:

wordsByLength[words[i].length()] = words[i];

where words[i] will be a vector. i’m also assuming that i will need to modify the = operator later on but i’m not so sure

  • 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-06-13T12:57:33+00:00Added an answer on June 13, 2026 at 12:57 pm

    Think about what your subscript operator shall return. Is it int&? The simplest choice would be HashEntry:

    template <typename HashedObj, typename storedObj >
    class HashTable
    {
    public:
        HashEntry& operator [] (int nIndex) // read/write version
        {
            return array[nIndex];
        }
    
        const HashEntry& operator [] (int nIndex) const // read only version
        {
            return array[nIndex];//THIS IS WHATS WRONG
        }
    ...
    };
    

    But it is private. So either make it public – but this breaks somehow your encapsulation.

    Because your are inserting HashedObj – then probably this is your desired return type:

    template <typename HashedObj, typename storedObj >
    class HashTable
    {
    public:
        HashedObj& operator [] (int nIndex) // read/write version
        {
            return array[nIndex].element;
        }
    
        const HashedObj& operator [] (int nIndex) const // read only version
        {
            return array[nIndex].element;
        }
    ...
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a config file that I want to basically edit the uncommented lines,
I have a relatively big SSIS package which I'm trying to edit. I basically
I basically have a program that filters records from one excel file to another
I am having a bear of a time getting this to work. I have
I basically have an edit profile page that along with other fields has a
I'm new at MVC and can't get this to work. I basically have a
Been trouble-shooting this for a few days now and have basically run dry of
I have (basically) this code - <script type=text/javascript language=javascript> $(document).ready(function() { $(#loadDiv).load(mypage.html, function(){ alert($(#someText).text())
I have basically two questions. How do I locate the default Rprofile which is
I have basically the same problem outlined in this question, however I am using

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.