如何return hash table c++ 中的出现次数?

How to return number of occurrences in hash table c++?

我有一个文本文件,我使用插入方法将所有单词插入哈希 table,然后我确实找到了假定 return 所写单​​词出现次数的方法,但确实如此不是 return 任何东西,什么都可能是错的。因此,如果 if 条件中的单词进入它,但我不确定如何正确 return 值变量

您已完成大部分工作,只需要稍作修改和稍作改动。

首先是修复。在HashNode

class HashNode
{
public:
    string key;
    int value;// constuctor accepts and assigns an int, not a string

public:
    HashNode(string key, int value)
    {
        this->key = key;
        this->value = value;
    }
    friend class HashTable; // not much point to friendship when the class 
                            // is entirely public
};

此更改稍后变得非常重要,因为现在 insert 我们可以

void insert(string key)
{
    int value=1; //start with a count of 1.
    int index = hashCode(key) % this->capacity; 

    for (list<HashNode>::iterator it = buckets[index].begin();
         it != buckets[index].end(); 
         ++it)
        if (it->key == key)
        {
            it->value++; // count another instance 
            return;
        }
// rest unchanged

现在我们可以显示散列中每个单词的计数 table。我们所要做的就是添加一两个功能,以便我们可以显示它。