C++ 如何获得哈希表的最大链的长度?

C++ 如何获得哈希表的最大链的长度?,c++,hash,hashtable,C++,Hash,Hashtable,我有一个如下所示的哈希表程序 #include<bits/stdc++.h> #include <algorithm> std::string fileName; std::fstream readFile; const int arraySize = 100; int storeFile[arraySize]; class Hash { std::list<int> *table; std::list<int>::it

我有一个如下所示的哈希表程序

#include<bits/stdc++.h> 
#include <algorithm>
std::string fileName;
std::fstream readFile;
const int arraySize = 100;
int storeFile[arraySize]; 

class Hash 
{ 
    std::list<int> *table; 
    std::list<int>::iterator anItetator;
public:  
    int count = 0;
    int mod = 100; 
    Hash();
    Hash(int Value);  

    void insertItem(int key); 

    int hashFunction(int key) { 
        return (key % mod); 
    } 
  
    void loopHash(); 

    void displayHash(); 
};

Hash::Hash(){

} 

Hash::Hash(int b) 
{ 
    this->mod = b; 
    table = new std::list<int>[mod]; 
} 
  
void Hash::insertItem(int key) 
{ 
    int index = hashFunction(key); 
    table[index].push_back(key);  
} 

void Hash::displayHash() { 
  for (int i = 0; i < mod; i++) { 
    std::cout << i; 
    for (auto x : table[i]) 
      std::cout << " --> " << x; 
    std::cout << std::endl; 
  } 
}

void Hash::loopHash() {

for(anItetator = table->begin(); anItetator != table->end(); ++anItetator){

   if(table->empty()==true) {
    count++;
    }

   std::cout << "Total number of empty entries is: " << count;
   std::cout << "The largest chain is: "  << *std::max_element(table->begin(),table->end());
}

}

int main(int argc, char *argv[])
{
     int n = arraySize;

     Hash h(h.mod); 

     std::cout << "Please enter the name of the file: " << std::endl;
     std::cin >> argv[0];                                            

     fileName = argv[0];

     std::cout << "Attempting to read file " << fileName << std::endl;

     readFile.open(fileName); 

for(int i = 0; i < n; i++) {
 
     while (readFile >> storeFile[i])
     {
        h.insertItem(storeFile[i]); 
     }
}
     //h.displayHash();     
     h.loopHash();
     readFile.close();
     return 0;
}
#包括
#包括
std::字符串文件名;
std::fstream读取文件;
常数int arraySize=100;
int-storeFile[arraySize];
类散列
{ 
标准::列表*表格;
std::list::迭代器anItetator;
公众:
整数计数=0;
int mod=100;
Hash();
散列(int值);
无效插入项(int键);
int哈希函数(int键){
返回(键%mod);
} 
void loopHash();
void displayHash();
};
Hash::Hash(){
} 
哈希::哈希(intb)
{ 
这个->模=b;
表=新标准::列表[mod];
} 
void Hash::insertItem(int键)
{ 
int index=hashFunction(键);
表[索引]。推回(键);
} 
无效哈希::显示哈希(){
对于(inti=0;ifor(anItetator=table->begin();anItetator!=table->end();++anItetator){
迭代第一个
std::list
中的元素,因为
table
是指向
列表的指针(但是,
list
预计将成为
mod
这样的列表中的第一个,有效地形成
list
s的数组)

要检查哈希表中的每个bucket,您应该做的是将access
table
作为一个数组,使用您已知的调用
new
的数组长度来分配内存;例如

int num_empty_buckets = 0;
int longest_chain = 0;
for (int index = 0; index < mod; ++index) {
    if (table[index].empty())
        ++num_empty_buckets;
    longest_chain = std::max(table[index].size(), longest_chain);
}
// print 'em...
int num\u empty\u bucket=0;
int最长链=0;
对于(int index=0;index与你的问题无关,但请不要使用在线竞赛网站来学习好的代码风格和良好习惯,因为他们教的是相反的。我还建议你去学习或学习几堂课来正确地学习C++,因为你的代码> Loopase< /Cult>函数包含了一些相当严重的逻辑问题。更不用说你了。r使用
argv[0]
作为临时存储非常糟糕。这是谁教你的?我想你需要在
loopHash
中的
if
条件下使用
anItetator
?!顺便说一句,它的拼写是迭代器。最近流行使用
argv[0]
用于存储东西。不要这样做-这不是它的用途。只需编写
std::cin>>fileName;
@molbdnilo:我想这是复制粘贴编程的一种症状。一个人做了一些奇怪/错误的事情,突然间你会越来越多地看到它。