使用来自另一个类的函数或此类中的函数中的结构
Use function from another class or struct in a function within this class
我正在创建一个哈希表,需要使用不同的哈希函数测试这个链式哈希表。我有哈希结构,例如
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)
相关文章:
- 使用不带参数的函数访问结构元素
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 链表,反向函数,数据结构
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 需要帮助将结构数组传递给函数
- 使用cpp中的结构和函数的多个学生条目
- 如何传递带有通过引用传递的结构参数的函数?
- 在类构造函数中使用结构变量
- 在 c++ 中将变量作为结构构造函数中的引用传递
- 在类构造函数中定义结构变量的参数
- 如何在模板类函数中分配结构值?
- 我应该在C++中将这些结构用作参数化构造函数吗?
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 在结构外部调用指向函数的指针
- 函数:结构的数组
- C++错误:只能为对象和函数结构指定存储类
- 将libuv与函数结构一起使用,而不是函数回调
- 从c#调用c++函数-结构体,指针,函数指针
- 函数结构数组输入cin问题