Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <iostream>
#include <vector>
using namespace std;

class HashTableExample 
{
  private:

    int currentSize;
    int maxSize;
    vector<string> keys;
    vector<string> vals;

    int hashkey(string key) 
    {
      return hash<string>{}(key) % maxSize;
    }

  public:

    HashTableExample() {}

    HashTableExample(int capacity)
    {
      currentSize = 0;
      maxSize = capacity;

      for(int i = 0; i < maxSize; i++)
      {
        keys.push_back("");
        vals.push_back("");
      }
    }

    string toString()
    {
      int k = keys.size();
      int v = vals.size();

      string str = "";
      str += "HashTableExamle [currentSize=" + to_string(currentSize);
      str += ", maxSize=" + to_string(maxSize) + 
      
      str += ", keys=";
      for (int i = 0; i < k; i++)
      {
        str += keys[i] + " ";
      }

      str += ", vals=";
      for (int i = 0; i < v; i++)
      {
        str += vals[i] + " ";
      }

      return str;
    }

    void makeEmpty()
    {
      currentSize = 0;
      for(int i = 0; i < maxSize; i++)
      {
        keys[i] = "";
        vals[i] = "";
      }
    }

    int getSize()
    {
      return currentSize;
    }

    bool isFull()
    {
      return currentSize == maxSize;
    }

    bool isEmpty()
    {
      return getSize() == 0;
    }

    bool contains(string key)
    {
      return get(key) != "";
    }

    void insert(string key, string val)
    {
      int tmp = hashkey(key);
      int i = tmp;

      do 
      {
        if (keys[i] == "") 
        {
          keys[i] = key;
          vals[i] = val;
          currentSize++;
          return;
        }

        if (keys[i] == key) 
        {
          vals[i] = val;
          return;
        }
        
        i = (i + 1) % maxSize;
      } 
      while (i != tmp);
    }
    
    string get(string key) 
    {
      int i = hashkey(key);
      
      while (keys[i] != "") 
      {
        if (keys[i] == key)
          return vals[i];
        i = (i + 1) % maxSize;
      }
      
      return "";
    }
    
    /** Function to remove key and its value **/
    void remove(string key) 
    {
      if (!contains(key))
        return;
        
      /** find position key and delete **/
      int i = hashkey(key);
      
      while (!(key == keys[i]))
        i = (i + 1) % maxSize;
      
      keys[i] = vals[i] = "";
      
      /** rehash all keys **/
      for (i = (i + 1) % maxSize; keys[i] != ""; i = (i + 1) % maxSize) 
      {
        string tmp1 = keys[i], tmp2 = vals[i];
        keys[i] = vals[i] = "";
        currentSize--;
        insert(tmp1, tmp2);
      }
      
      currentSize--;
    }
    
    /** Function to print HashTable **/
    void printHashTable() 
    {
      cout << endl << "Hash Table: " << endl;
      for (int i = 0; i < maxSize; i++)
        if (keys[i] != "")
          cout << keys[i] << " " << vals[i] << endl;
      cout << endl;
    }
};

int main() 
{
  cout << "Hash Table" << endl << endl;
  cout << "Enter size" << endl;
  int size;
  cin >> size;
  
  HashTableExample hash(size);

  char ch;
  do 
  {
    cout << "\nHash Table Operations\n" << endl;
    cout << "1. insert " << endl;
    cout << "2. remove" << endl;
    cout << "3. get" << endl;
    cout << "4. clear" << endl;
    cout << "5. size" << endl;
    cout << "6. exit" << endl;
    int choice;
    cin >> choice;

    string key, value;
    cout << endl;
    switch (choice) 
    {
      case 1:
        cout << "Enter key and value" << endl;
        cin >> key >> value;
        hash.insert(key, value);
        break;
      case 2:
        cout << "Enter key" << endl;
        cin >> key;
        hash.remove(key);
        break;
      case 3:
        cout << "Enter key" << endl;
        cin >> key;
        cout << "Value = " << hash.get(key) << endl;
        break;
      case 4:
        hash.makeEmpty();
        cout << "Hash Table Cleared\n" << endl;
        break;
      case 5:
        cout << "Size = " + to_string(hash.getSize()) << endl;
        break;
      default:
        cout << "Wrong Entry \n " << endl;
        break;
    }

    /** Display hash table **/
    hash.printHashTable();
    cout << "Do you want to continue (Type y or n) " << endl;
    cin >> ch;
  } 
  while (ch == 'Y' || ch == 'y');
}


What I have tried:

errors cannot remove hash error occur and to_string error occur
Posted
Updated 15-Sep-20 21:50pm
v2
Comments
OriginalGriff 16-Sep-20 2:54am    
Where are the errors?
What error messages do you get?
What did you do to get them?
What have you tried to fix them?
SAMI MUSHTAQ 16-Sep-20 3:32am    
t0_string not declare in this scope
hash not declare in this scope
Richard MacCutchan 16-Sep-20 3:44am    
Which lines? I cannot find any reference to t0_string. And you have not defined a hash object for the insert, remove etc. functions.
CPallini 16-Sep-20 3:48am    
The posted code compiles and runs fine on my Linux box.

1 solution

SAMI MUSHTAQ:
t0_string not declare in this scope
hash not declare in this scope

If I try to compile your code using an online C++ compiler: https://www.onlinegdb.com/online_c++_compiler[^]
It finds no compilation errors, indeed it appears to run.
So either you have shown us the wrong code, or you are compiling the wrong code.

We can't help you with either of those!
 
Share this answer
 
Comments
Stefan_Lang 16-Sep-20 5:48am    
I can confirm this, although I would have expected that the code needs an #include <string>. Maybe that is the problem: while some implementations may include <string> within iostream, others might not.
OriginalGriff 16-Sep-20 6:01am    
string is part of std - std::string so the "using namespace std" should cover it.
http://www.cplusplus.com/reference/string/string/
Stefan_Lang 16-Sep-20 6:07am    
I know that, what I meant is that to use string - with or without std:: - you normally need to #include <string>. He didn't. This works on some compilers, if they do include string within the iostream header, but the C++ specification does not require this. Therefore including only iostream may not be enough, dependend on the compiler.

See here: https://stackoverflow.com/questions/16506095/do-i-have-to-use-include-string-beside-iostream

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900