Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 哈希表(线性探测)_C++ - Fatal编程技术网

C++ 哈希表(线性探测)

C++ 哈希表(线性探测),c++,C++,我正在使用线性探测制作一个哈希表,当加载因子(输入哈希表的元素数)/(哈希表大小)大于0.5时,我必须调整数组的大小,我必须调整数组的大小。我通过初始化包含哈希表相关函数的类中的指针来调整大小。我将指针放在一个大小为100的结构(结构仅包含字符串)数组中。每次加载因子大于0.5时,我通过创建一个比以前大一倍的新数组来调整数组的大小,并将指针指向新数组。我还有一个int,它存储数组的当前大小,并随使用resize函数的每个实例而更新。插入的元素数随着每次调用insert函数而增加。我是否这样做正确

我正在使用线性探测制作一个哈希表,当加载因子(输入哈希表的元素数)/(哈希表大小)大于0.5时,我必须调整数组的大小,我必须调整数组的大小。我通过初始化包含哈希表相关函数的类中的指针来调整大小。我将指针放在一个大小为100的结构(结构仅包含字符串)数组中。每次加载因子大于0.5时,我通过创建一个比以前大一倍的新数组来调整数组的大小,并将指针指向新数组。我还有一个int,它存储数组的当前大小,并随使用resize函数的每个实例而更新。插入的元素数随着每次调用insert函数而增加。我是否这样做正确吗?下面是我的代码

#include <cstring>
#include <vector>
#include <math.h>
#include <iomanip>

using namespace std;

int power(int a,int b)
{
   for (int i=0;i<b;i++)
   {
       a*=a;
   }
   return a;
};

struct Bucket
{
    string word;
};   

const int size=100;

class LProbing
{
  private:
          int a; //a constant which is used in hashing
          int cursize;  //current size of hash table
          Bucket *Table;  //pointer to array of struct
          int loadfactor; //ratio of number of elements entered over size of hashtable
          int n;  //number of elements entered
          Bucket table[size];   //array of structs
  public:
          LProbing(int A);  //constant is decided by user 
          void resize();
          void insert(string word);
          void Lookup(string word);
};

LProbing::LProbing(int A)
{  
   cursize=size;                   
   a=A;
   Table=table;
   loadfactor=0;  //initially loadfactor is 0 as number of elements entered are 0
   n=0;
}

void LProbing::resize() 
{
    cout<<"resize"<<endl;
    loadfactor=n/cursize;   //ensuring if resize needs to be done
    if (loadfactor<=0.5)
    {
       return;
    }                                      
    const int s=2*cursize;  
    Bucket PTable[s];
    for (int i=0;i<cursize;i++)
    {
        if (Table[i].word.empty())
        continue;

        //rehashing the word onto the new array
        string w=Table[i].word;    
        int key=0;
        for (int j=0;j<w.size();j++)
        {
           unsigned char b=(unsigned char)w[j];
           key+=(int)power(a,i)*b;
        }
        key=key%(2*cursize);
        PTable[key].word=w;  //entering the word in the new array
    }
    Table=PTable;  //putting pointer equal to new array
    cursize=2*cursize;  //doubling the current size of array
}

void LProbing::insert(string word)
{
   cout<<"1"<<endl; 
   n++;  //incrementing the number of elements entered with every call to insert

    //if loadfactor is greater than 0.5, resize array
   loadfactor=n/cursize;
   if (loadfactor>0.5)
   {               
       resize();
    }                
   //hashing the word
   int k=0;
   for (int i=0;i<word.size();i++)
   {
       unsigned char b=(unsigned char)word[i];
       int c=(int)((power(a,i))*b);
       k+=c;
       cout<<c<<endl;
   }

   int key=0;
   key=k%cursize;
   cout<<key<<endl;
   //if the respective key index is empty enter the word in that slot
   if (Table[key].word.empty()==1)
   {
       cout<<"initial empty slot"<<endl;
       Table[key].word=word;
   }
   else  //otherwise enter in the next slot
   {
       //searching array for empty slot
       while (Table[key].word.empty()==0)
       {
        k++;
        key=k%cursize;
       }
       //when empty slot found,entering the word in that bucket
       Table[key].word=word;
       cout<<"word entered"<<endl;
   }
}             

#include "Linear Probing.cpp"
#include <fstream>

using namespace std;

int main() 
{
   LProbing H(35);
   ifstream fin;
   fin.open("dict.txt");
   vector<string> D;

   string d;
   while (getline(fin,d))
   {
       if (!d.empty())
       {
           D.push_back(d);
       }
   }
   fin.close();
   for (int i=0;i<D.size();i++)
   {
       H.insert(D[i]);
   }
   system("PAUSE");
   return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
整数功率(整数a、整数b)
{

对于(int i=0;i您正在处理大数字,变量“key”溢出:

key += (int)power(a,i)*b

您处理的是大数字,变量“key”溢出:

key += (int)power(a,i)*b

看起来loadfactor的计算方式是
int/int
,因此它将保持0直到达到1。尝试将分区的输入转换为浮点数。

看起来loadfactor的计算方式是
int/int
,因此它将保持0直到达到1。尝试将分区的输入转换为浮点数。

您可能会发现这很有帮助mewhere:

您可能会发现它在某些地方很有用:

让自己相信它在起作用(或不起作用),构造一套单元测试。当我在一个单词文件上运行这段代码时,我有时会得到key的负值。我不明白为什么会发生这种情况,因为我没有减去任何东西,我先将每个字母转换为无符号字符,然后再计算它的值。由于key为负数,程序会给出一个错误。为了说服您如果它起作用(或不起作用),构造一套单元测试。当我在一个单词文件上运行这段代码时,我有时会得到key的负值。我不明白为什么会发生这种情况,因为我没有减法任何东西,我先将每个字母转换为无符号字符,然后再计算其值。由于key为负数,程序会给出一个错误。我给出了一个positive输入。我将附加主文件。我不明白你想说什么??我只想给a一个随机值,所以如果我给它任何值都可以。是的,我只是显示了键的值,这正是发生的事情。但是现在程序在数组调整大小后立即被卡住。我没有足够的声誉。只是一个新手。我是giv我将附加主文件我不明白你想说什么?我只想给a一个随机值,所以如果我给它任何值都可以。是的,我只是显示了key的值,这正是发生的事情。但是现在程序在数组调整大小后立即被卡住我没有足够的声誉。只是一个新的e、 虽然这在理论上可以回答问题,但在此处包含答案的基本部分,并提供链接供参考。虽然这在理论上可以回答问题,但在此处包含答案的基本部分,并提供链接供参考。