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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:16:00+00:00 2026-06-14T09:16:00+00:00

I’m having difficulty assigning a value to a string. The string is contained in

  • 0

I’m having difficulty assigning a value to a string. The string is contained in a struct. Here is the full code. It’s largely incomplete(I’m trying to get this to work before fixing my iterator)

It fails at operator[]

:

#include <string>
#include <iostream>
#include <vector>

using namespace std;

template <class Key,class Value>
class hashMap
{
  public:
    explicit hashMap( int size = 101 ) : arraySize( size ){
      array.reserve(arraySize+1);
    }
    hashMap(const hashMap & rhs){
      arraySize = rhs.arraySize;
      array.reserve(arraySize);
      for(int i = 0; i < arraySize; i++)
      {
        array[i] = rhs.array[i];
      }
    }
    ~hashMap()
    {
    }
    hashMap & operator=(const hashMap & rhs){
      if (&rhs != this)
      {
        arraySize = rhs.arraySize;
        array.clear();
        array.reserve(arraySize);
        for(int i = 0; i < arraySize; i++)
        {
          array[i] = rhs.array[i];
        }
      }
    }
    Value & operator[](const Key & key)
    {
        unsigned long long pos = hash(key, arraySize);
        unsigned long long quad = 1;
        while (array[pos].active != false && array[pos].first != key)
        {
          pos += quad;
          pos %= arraySize;
          quad *= 2;
        }
        array[pos].first = key; // FAILS HERE
        array[pos].active = true;
        return array[pos].second;
    }

    struct cell{
      Key first;
      Value second;
      bool active;
      cell(){
        active = false;
      }
    };

    class const_iterator
    {
      public:
        cell operator*()
        {
          return array[pos];
        }
      private:
        int pos;
    };

  private:
    vector<cell> array;
    int arraySize;
    int hash(const std::string & key, int tableSize)
    {
      int hashVal = 0;

      for(int i = 0; i < key.length(); i++)
      {
        hashVal = 37 * hashVal + key[i];
      }

      hashVal %= tableSize;
      if(hashVal < 0)
      {
        hashVal += tableSize;
      }

      return hashVal;
    }
    int hash(int key, int tableSize)
    {
      return key%tableSize;
    }
};

I would very much appreciate the help!

~A hopeless computer science student

  • 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-14T09:16:01+00:00Added an answer on June 14, 2026 at 9:16 am

    In your constructors you use

    array.reserve(arraySize);
    

    but std::vector::reserve only reserves some memory for easy array expansion and does not create elements (nor resizes array). So, your cells don’t get constructed.

    Then, when you are trying to assign something to array, you are actually go out of array range.

    You should use std::vector::resize instead of std::vector::reserve (as a simplest solution)

    array.resize(arraySize);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
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

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.