GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::unordered_map::hash_function - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. unordered_map
  3. unordered_map
  4. hash_function

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<unordered_map>

std::unordered_map::hash_function(C++11)

hasher hash_function() const;           // (1) C++11
constexpr hasher hash_function() const; // (1) C++26

概要

コンテナのハッシュ関数オブジェクトを返す

戻り値

コンテナのハッシュ関数オブジェクト。

このメンバ関数で返されるハッシュ関数オブジェクトは、コンストラクタ、あるいは、直近の代入(コピー、あるいはムーブ)、交換でコンテナに保存されたオブジェクトのコピーである。

計算量

定数

備考

戻り値の型である、ハッシュ関数オブジェクトの型 hasher は、unordered_map のメンバ型で、三番目のテンプレートパラメータ Hash を別名定義したものである。

ハッシュ関数オブジェクトは、名前の通りハッシュ値を算出するためのオブジェクトで、キー値が与えられると std::size_t の値を返すメンバ関数 std::size_t operator()(key_type) を持つ必要がある。

また、key_equal が true を返すキー値に対しては、それらのキー値それぞれに対してハッシュ関数オブジェクト hasher が返す値も等しくなければならない。

なお、key_equal が false を返すキー値に対しては、それらのキー値それぞれに対してハッシュ関数オブジェクト hasher が返す値は必ずしも異なる必要はないが、可能な限り異なる値を返すべきである。

テンプレートパラメータを省略した場合、hasher はデフォルト値 std::hash<key_type> となる。

例

#include <iostream>
#include <string>
#include <unordered_map>

int main()
{
  std::unordered_map<std::string, int> um = {
    {"1st", 1},
    {"2nd", 2},
    {"3rd", 3}
  };

  decltype(um)::hasher hash{ um.hash_function() };

  std::cout << "hash(\"1st\") = " << hash("1st") << std::endl;
  std::cout << "hash(\"2nd\") = " << hash("2nd") << std::endl;
}

出力例

hash("1st") = 4827876729863105478
hash("2nd") = 12978775524054262047

バージョン

言語

  • C++11

処理系

  • Clang: 3.1 ✅
  • GCC: 4.7.0 ✅
  • Visual C++: ?

関連項目

名前 説明
hash デフォルトのハッシュ関数オブジェクト(class template)
key_eq キー比較用関数オブジェクトの取得

参照

  • P3372R3 constexpr containers and adaptors
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.