我是OOP和数据结构的新手。我正在使用C ++算法来比较一个字符串句子和另一个字符串句子。我们的想法是从两个字符串中提取单个单词,将它们存储在两个不同的哈希表中,如果第一个字符串中的单词出现在第二个字符串中,则使用布尔值确定。必须有很多方法来解决这个问题,但就我的目的而言,这种方法应该使用哈希表。我创建了一个名为hashClass的类,它有一个构造函数和三个方法。第一个方法拆分字符串,第二个方法拆分单词split并将它们存储在哈希表中的索引处,第三个方法应该确定第一个字符串(Magazine对象)的单词是否出现在第二个字符串(Note对象)中。我被困在第三个主要是因为我不知道如何比较存储在每个对象的哈希表中的内容。这是我的main.cpp的代码:
int main()
{
hashClass Magazine;
hashClass Note;
Magazine.SplitString("give me one grand today night");
Note.SplitString("igev em eno drnag today night");
bool IsMagReplica(const hashClass Magazine, const hashClass Note)
{
//what can I do here to compare the contents of both hash tables which are stored as objects of a class?
}
return 0;
}
这是我的hash.h的代码:
class hashClass
{
public:
hashClass();
void SplitString(string str);
int HashWord(string word, int length);
bool IsMagReplica(hashClass htMag, hashClass htNote);
private:
static const int tableSize = 52;
struct item
{
string word;
int length;
item* next;
};
item* HashTable[tableSize];
};
如果您还需要查看hash.cpp代码,请告诉我。非常感谢任何帮助。希望我的解释清晰简洁。感谢。
答案 0 :(得分:0)
不能在评论中使用它,所以这里是:
hashclass split(const std::string & str)
这样的东西。你给它一个字符串,它给你一个填写的哈希表。价值回报是三级法则对你非常重要的原因。在这一点上,'a还值得阅读The Rule of Five和Copy Elision。split
中的smurph,以便您知道它有效。 bool replica(const std::string & strA, const std::string & strB)
split
来获取两个哈希表。 所以要做到这一切,哈希表必须能够
如果一切都很好地解耦,你可以单独测试每一块,这样就可以更容易地找出错误。