c++ - 在此类中的函数中使用另一个类中的函数或结构

标签 c++ class c++11 hashtable

我正在创建一个哈希表,需要用不同的哈希函数测试这个链式哈希表。 我有哈希结构,例如

struct Hasher {
    virtual int hash(std::string s, int N) = 0;
};

struct SumHasher : Hasher {
    int hash(std::string s, int N){
        int result = 0;
        for (int i=0; i<s.size(); ++i)
            result += s[i];
        return int (std::abs((int)result)) % N;
    }
};
struct ProdHasher : Hasher {
    int hash(std::string s, int N) {
        int result = 1;
        for (int i=0; i<s.size(); ++i)
        result *= s[i];
    return int (std::abs((int)result)) % N;
    }
};
struct ShiftHasher : Hasher {
    int hash(std::string s, int N){
    const int shift = 6; unsigned z = 0;
    const int mask = ~z >> (32-shift); // lower 6 bits on
    int result = 0;
    for (int i = 0; i < s.length(); i++)
        result = (result << shift) | (s[i] & mask);
    return int (std::abs((int)result)) % N;
    }
};

现在我如何通过创建结构散列类型然后将该对象传递给构造函数来在 Hashtable 类中使用此函数

class ChainedHashTable
{

    ListNode **T; // array of linked lists
    int capacity;
public:
    Hasher *myhash;
    int info;
    ChainedHashTable(int numberOfChains, Hasher *myHasher){
        myhash = hasher;
        capacity = numberOfChains;
       T = new ListNode* [capacity];
       for (int i=0; i<capacity; ++i)
           T[i] = NULL;
     }
.......
void ChainedHashTable::insert(std::string key, int info){
    int h = myhash::hash(key, capacity);
    T[h] = ListNode::make(key, info, T[h]);
}

最佳答案

你应该使用:

myhash->hash(key, capacity)

关于c++ - 在此类中的函数中使用另一个类中的函数或结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21820566/

相关文章:

c++ - isdigit() 无法将 "1"识别为数值

Java字符串方法错误

qt - 接口(interface)类纯虚信号的连接

c++ - 使用基于范围的 for 循环时需要迭代器

swift - 从 GameScene 类中的节点创建子类

c++ - 向链表添加空格

c++ - 如何将小部件与我的类(class)绑定(bind)?

c++ - atomic<T*> 总是无锁的吗?

c++ - 单线程时不会发生多线程时指针数组的奇数内存泄漏

java - Android:何时开始使用 .在 Android list 中?