C++ 检查哈希表中是否存在重复项
我试图读取一个文件,每个字符串将少于30个,在数千个字符串中,将有20个唯一的序列。我们正在计算unique在哈希表中出现的次数。我在处理碰撞时遇到困难。我将所有char[]值初始化为0,但是IfProteins[key]。proteen==0无法检查结构中的该点的值是否为0或我的某个proteins始终为ABCDJ。。。超过10个,少于30个字符。因此,我认为将all初始化为0将是一种查看是否已在结构中放入protein的方法 这个逻辑错误在我的第二个if语句中 这是我们应该使用的算法,然后是我的代码C++ 检查哈希表中是否存在重复项,c++,hashtable,C++,Hashtable,我试图读取一个文件,每个字符串将少于30个,在数千个字符串中,将有20个唯一的序列。我们正在计算unique在哈希表中出现的次数。我在处理碰撞时遇到困难。我将所有char[]值初始化为0,但是IfProteins[key]。proteen==0无法检查结构中的该点的值是否为0或我的某个proteins始终为ABCDJ。。。超过10个,少于30个字符。因此,我认为将all初始化为0将是一种查看是否已在结构中放入protein的方法 这个逻辑错误在我的第二个if语句中 这是我们应该使用的算法,然后是
While(there are proteins)
Read in a protein
Hash the initial index into the proteins table
While(forever)
If(found key in table)
Increment count
Break;
If(found empty spot in table)
Copy key into table
Increment count
Break;
Increment index; // collision! Try the next spot!
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//struct to hold data and count
struct arrayelement
{
char protien[30] {"0"};
int count;
};
arrayelement protiens[40];
//hash function A=65 ascii so 65-65=0 lookup table = A=0,B=1...
//h(key) = ( first_letter_of_key + (2 * last_letter_of_key) ) % 40
int getHashKey(char firstLetter, char lastLetter)
{
return ((int(firstLetter) - 65) + (2 * (int(lastLetter) - 65))) % 40;
}
int main()
{
fstream file;
string filename;
char word[30];
int key;
filename = "proteins.txt";
//open file
file.open(filename.c_str());
//while not eof
while (file >> word)
{
//get key
key = getHashKey(word[0], word[strlen(word)-1]);
//loop "forever" no difference if i use 1 or 10000000 besisdes run time????
for (int j = 0; j < 1; j++)
{
//if found key in table
if (protiens[key].protien == word)
{
protiens[key].count++;
break;
}
//if found empty spot in table
//if(protiens[key].protien == "0") i intialized all protiens to "0" why would this not work for
//checking if i put a protien there already or not
else
{
strcpy_s(protiens[key].protien, word);
protiens[key].count++;
break;
}
//collison incrment key
key = getHashKey(word[0], word[strlen(word) - 1]) + 1;
}
}
//print array of uniques with counts
for (int j = 0; j < 40; j++)
{
cout << j << "\t" << protiens[j].protien << "\t" << protiens[j].count << endl;
}
}
由于0是常量,而proteins[key].proteen是指向变量的指针,因此它们不可能相等
想象一下,如果他们是平等的。这意味着protients[key].protien[0]='Q';与0[0]='Q';完全相同;。但前者是完全合理的,改变一个变量。后者是疯狂的,修改一个常数
我不知道当你有std::string的时候为什么要这样使用字符数组。但如果您坚持这样做,请使用strcmp来比较字符串。比较指针和字符是否相等,可以告诉你这两个指针是否相等,而不是它们是否指向相同的字符串。我的老师自97年以来没有改变课程,所以我自学数据结构,他说使用字符[],在我上节课中,我们会使用字符串。。。我会调查一下strcmp,谢谢你解释我的逻辑错误。
//if(protiens[key].protien == "0") i intialized all protiens to "0" why would this not work for
//checking if i put a protien there already or not