GeezerVison is showing cpprefjp.github.io — std::hash - cpprefjp C++日本語リファレンス
最終更新日時:
2026年08月19日 20時54分14秒 (JST)
Akira Takahashi
が更新
履歴
編集
class template
<string>
std:: hash (C++11)
namespace std {
// 標準のallocatorを使用するbasic_string に対する明示的特殊化
template <> struct hash < string > ; // (1) C++11からC++20まで
template <> struct hash < u8string > ; // (2) C++20からC++20まで
template <> struct hash < u16string > ; // (3) C++11からC++20まで
template <> struct hash < u32string > ; // (4) C++11からC++20まで
template <> struct hash < wstring > ; // (5) C++11からC++20まで
// pmr::polymorphic_allocatorを使用するbasic_string に対する明示的特殊化
template <> struct hash < pmr::string > ; // (6) C++17からC++20まで
template <> struct hash < pmr::u8string > ; // (7) C++20からC++20まで
template <> struct hash < pmr::u16string > ; // (8) C++17からC++20まで
template <> struct hash < pmr::u32string > ; // (9) C++17からC++20まで
template <> struct hash < pmr::wstring > ; // (10) C++17からC++20まで
// 任意のアロケータを許す部分特殊化((1)〜(10)を統合)
template < class Allocator >
struct hash < basic_string < char , char_traits < char > , Allocator >> ; // (11) C++23
template < class Allocator >
struct hash < basic_string < char8_t , char_traits < char8_t > , Allocator >> ; // (12) C++23
template < class Allocator >
struct hash < basic_string < char16_t , char_traits < char16_t > , Allocator >> ; // (13) C++23
template < class Allocator >
struct hash < basic_string < char32_t , char_traits < char32_t > , Allocator >> ; // (14) C++23
template < class Allocator >
struct hash < basic_string < wchar_t , char_traits < wchar_t > , Allocator >> ; // (15) C++23
}
概要
std::hash クラスの、basic_string に対する特殊化。文字列の内容からハッシュ値を計算し、basic_stringをunordered_map やunordered_set のキーとして使用できるようにする。
各文字型に対する特殊化が提供される。定義の形式は、C++のバージョンによって以下のように変遷している。
(1)-(5) : 標準のallocator を使用するbasic_string(string 等)に対する明示的特殊化。char・char16_t・char32_t・wchar_t版がC++11で、char8_t版 (2) がC++20で追加された。
(6)-(10) : pmr::polymorphic_allocator を使用するbasic_string(pmr::string等)に対する明示的特殊化。char・char16_t・char32_t・wchar_t版がC++17で、char8_t版 (7) がC++20で追加された。
(11)-(15) : C++23で、(1)〜(10)を統合した、任意のアロケータAllocatorを許す部分特殊化。ハッシュ値は文字列の内容にのみ依存しアロケータには依存しないため、標準以外のアロケータを使用するbasic_stringもハッシュ可能となった。
効果
Sを対象の文字列型、SVを対応する文字列ビュー型(例えばSがstring ならSVはstring_view )、sを型Sのオブジェクトとするとき、hash<S>()(s)はhash<SV>()(SV(s))と等しい。
つまり、同じ文字列内容を表すオブジェクト同士は、文字列型・文字列ビュー型・アロケータの違いにかかわらず、同じハッシュ値を持つ。
例
出力
バージョン
言語
関連項目
参照