Click here to Skip to main content
14,633,928 members
Rate this:
Please Sign up or sign in to vote.
See more:
#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 2 days ago
v2
Comments
OriginalGriff 2 days ago
   
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 2 days ago
   
t0_string not declare in this scope
hash not declare in this scope
Richard MacCutchan 2 days ago
   
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 2 days ago
   
The posted code compiles and runs fine on my Linux box.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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!
   
Comments
Stefan_Lang 2 days ago
   
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 2 days ago
   
string is part of std - std::string so the "using namespace std" should cover it.
http://www.cplusplus.com/reference/string/string/
Stefan_Lang 2 days ago
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100