public member type
<unordered_set>

std::unordered_set::hash_function

hasher hash_function() const;
獲取雜湊函式
返回由unordered_set容器使用的雜湊函式物件。

雜湊函式是一個一元函式,它接受一個型別為key_type作為引數,並基於它返回一個唯一的size_t型別值。它在構造時被容器採納(更多資訊請參見unordered_set的建構函式)。預設情況下,它是相應鍵型別的預設雜湊函式:hash<key_type>。

引數



返回值

雜湊函式。

成員型別hasher是容器使用的雜湊函式的型別,在unordered_set中定義為其第二個模板引數(Hash).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// unordered_set::hash_function
#include <iostream>
#include <string>
#include <unordered_set>

typedef std::unordered_set<std::string> stringset;

int main ()
{
  stringset myset;

  stringset::hasher fn = myset.hash_function();

  std::cout << "that: " << fn ("that") << std::endl;
  std::cout << "than: " << fn ("than") << std::endl;

  return 0;
}

可能的輸出
that: 1046150783
than: 929786026


請注意,兩個相似的字串如何產生截然不同的雜湊值。

複雜度

常量。

迭代器有效性

沒有變化。

另見