0

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();
}

クラスメンバー関数で hash(key) を使用して両方のハッシュ関数をサポートできるように、myHashFunction または提供されたハッシュ関数をハッシュにバインドするにはどうすればよいですか? 完全に間違っている場合は、正しい道を教えてください。ありがとう。

4

2 に答える 2

0

なぜあなたは使わないのですstd::hashか?<functional>ヘッダーで定義されています。

于 2013-11-11T03:17:36.730 に答える