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::hash - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. hash

最終更新日時: 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))と等しい。

つまり、同じ文字列内容を表すオブジェクト同士は、文字列型・文字列ビュー型・アロケータの違いにかかわらず、同じハッシュ値を持つ。

例

#include <cassert>
#include <functional>
#include <string>
#include <string_view>

int main()
{
  std::string s = "hello";

  // 文字列の内容からハッシュ値を計算する
  std::size_t h = std::hash<std::string>{}(s);

  // 対応するstring_viewのハッシュ値と一致する
  assert(h == std::hash<std::string_view>{}(std::string_view{s}));
}

出力

バージョン

言語

  • C++11

関連項目

  • std::hash
  • std::basic_string

参照

  • LWG Issue 3705. Hashability shouldn't depend on basic_string's allocator
    • C++23で、hashのbasic_string特殊化が、アロケータ固定の明示的特殊化から任意のアロケータを許す部分特殊化に変更された
You are visitor 01402520 — 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.