我正在使用HashMap类声明中的typedef std :: function在HashMap类上进行工作。
typedef std::function<unsigned int(const std::string&)> HashFunction;
对于该类的私有成员,我有HashFunction哈希,可以与我自己的哈希函数一起使用,也可以使用提供给构造函数的其他函数。
HashFunction hash;
unsigned int myHashFunction(const std::string&) const;
默认构造函数必须将哈希设置为默认值,在我的情况下为myHashFunction。还有一个构造函数,其中HashFunction作为参数必须使用该特定函数,而不是myHashFunction。
HashMap::HashMap()
: map(new Node*[INITIAL_BUCKET_COUNT]), mapSize(0), mapCapacity(INITIAL_BUCKET_COUNT),
hash(std::bind(&HashMap::myHashFunction, this)) // This is definitely not correct
{
initializeMap();
}
HashMap::HashMap(HashFunction hashFunction)
: map(new Node*[INITIAL_BUCKET_COUNT]), mapSize(0), mapCapacity(INITIAL_BUCKET_COUNT),
hash(hashFunction) //Is this correct?
{
initializeMap();
}
如何将myHashFunction或提供的哈希函数绑定到哈希,以便可以在类成员函数中使用hash(key)来支持两个哈希函数?如果我完全弄错了,请引导我走正确的路。谢谢。
对于该std::bind
行,如果要绑定成员函数,则需要包含一个占位符,如下所示:
std::bind(&HashMap::myHashFunction, this, std::placeholders::_1);
但是,您可能会发现使myHashFunction
静态成员函数更好,除非它实际上在您的内部使用其他成员或数据HashMap
(我想可能不应该这样)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句