Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::hash

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
       
      Defined in header<bitset>
      Defined in header<coroutine>
      (since C++20)
      Defined in header<chrono>
      (since C++26)
      Defined in header<filesystem>
      (since C++17)
      Defined in header<functional>
      Defined in header<memory>
      Defined in header<optional>
      (since C++17)
      Defined in header<stacktrace>
      (since C++23)
      Defined in header<string>
      Defined in header<string_view>
      (since C++17)
      Defined in header<system_error>
      Defined in header<text_encoding>
      (since C++26)
      Defined in header<thread>
      Defined in header<typeindex>
      Defined in header<utility>
      (since C++26)
      Defined in header<variant>
      (since C++17)
      Defined in header<vector>
      template<class Key>
      struct hash;
      (since C++11)

      The enabled specializations of thehash template define a function object that implements ahash function.

      Given a typeKey, each specializationstd::hash<Key> is eitherenabled ordisabled :

      • Ifstd::hash<Key> is not provided by the program or the user, it is disabled.
      • Otherwise,std::hash<Key> is enabled if all following conditions are satisfied:
      • All following requirements are satisfied:
      • Given the following values:
      • h, an object of typestd::hash<Key>.
      • k1 andk2, objects of typeKey.
      All following requirements are satisfied:
      • Otherwise,std::hash<Key> is disabled.

      Disabled specializations do not satisfyHash, do not satisfyFunctionObject, and following values are allfalse:

      In other words, they exist, but cannot be used.

      Contents

      Nested types

      Type Definition
      argument_type(deprecated in C++17)Key
      result_type(deprecated in C++17)std::size_t
      (until C++20)

      [edit]Member functions

      constructs a hash function object
      (public member function)
      calculates the hash of the argument
      (public member function)

      [edit]Standard library specializations

      Each header that declares the templatestd::hash also provides enabled specializations ofstd::hash for the following types:

      Afreestanding implementation is required to provide these aforementioned specializations and the disabled-by-default specializations.

      (since C++20)

      On top of that, some headers also provide other enabledstd::hash specializations for library types (seebelow).

      For allstd::hash specializations provided by the standard library except the following, all their member functions arenoexcept:

      (since C++26)
      (since C++17)

      [edit]Specializations for library types

      Language support library
      hash support forstd::coroutine_handle
      (class template specialization)[edit]
      Dianostics library
      hash support forstd::error_code
      (class template specialization)[edit]
      hash support forstd::error_condition
      (class template specialization)[edit]
      hash support forstd::type_index
      (class template specialization)[edit]
      hash support forstd::stacktrace_entry
      (class template specialization)[edit]
      hash support forstd::basic_stacktrace
      (class template specialization)[edit]
      Memory management library
      hash support forstd::unique_ptr
      (class template specialization)[edit]
      hash support forstd::shared_ptr
      (class template specialization)[edit]
      hash support forstd::indirect
      (class template specialization)[edit]
      General utilities library
      hash support forstd::optional
      (class template specialization)[edit]
      hash support forstd::variant
      (class template specialization)[edit]
      hash support forstd::monostate
      (class template specialization)
      hash support forstd::bitset
      (class template specialization)[edit]
      Containers library
      hash support forstd::vector<bool>
      (class template specialization)
      Strings library
      hash support for strings
      (class template specialization)[edit]
      hash support for string views
      (class template specialization)[edit]
      Text processing library
      hash support forstd::text_encoding
      (class template specialization)[edit]
      Time library
      hash support forstd::chrono::duration
      (class template specialization)
      hash support forstd::chrono::time_point
      (class template specialization)
      hash support forstd::chrono::day
      (class template specialization)
      hash support forstd::chrono::month
      (class template specialization)
      hash support forstd::chrono::year
      (class template specialization)
      hash support forstd::chrono::weekday
      (class template specialization)
      hash support forstd::chrono::weekday_indexed
      (class template specialization)
      hash support forstd::chrono::weekday_last
      (class template specialization)
      hash support forstd::chrono::month_day
      (class template specialization)
      hash support forstd::chrono::month_day_last
      (class template specialization)
      hash support forstd::chrono::month_weekday
      (class template specialization)
      hash support forstd::chrono::month_weekday_last
      (class template specialization)
      hash support forstd::chrono::year_month
      (class template specialization)
      hash support forstd::chrono::year_month_day
      (class template specialization)
      hash support forstd::chrono::year_month_day_last
      (class template specialization)
      hash support forstd::chrono::year_month_weekday
      (class template specialization)
      hash support forstd::chrono::year_month_weekday_last
      (class template specialization)
      hash support forstd::chrono::zoned_time
      (class template specialization)
      hash support forstd::chrono::leap_second
      (class template specialization)
      Input/output library
      hash support forstd::filesystem::path
      (class template specialization)[edit]
      Concurrency support library
      hash support forstd::thread::id
      (class template specialization)[edit]

      [edit]Notes

      The actual hash functions are implementation-dependent and are not required to fulfill any other quality criteria except those specified above. Notably, some implementations use trivial (identity) hash functions which map an integer to itself. In other words, these hash functions are designed to work with unordered associative containers, but not as cryptographic hashes, for example.

      Hash functions are only required to produce the same result for the same input within a single execution of a program; this allows salted hashes that prevent collision denial-of-service attacks.

      There is no specialization for C strings.std::hash<constchar*> produces a hash of the value of the pointer (the memory address), it does not examine the contents of any character array.

      Additional specializations forstd::pair and the standard container types, as well as utility functions to compose hashes are available inboost::hash.

      [edit]Example

      Run this code
      #include <cstddef>#include <functional>#include <iomanip>#include <iostream>#include <string>#include <unordered_set> struct S{std::string first_name;std::string last_name;bool operator==(const S&)const=default;// since C++20}; // Before C++20.// bool operator==(const S& lhs, const S& rhs)// {//     return lhs.first_name == rhs.first_name && lhs.last_name == rhs.last_name;// } // Custom hash can be a standalone function object.struct MyHash{std::size_t operator()(const S& s)constnoexcept{std::size_t h1= std::hash<std::string>{}(s.first_name);std::size_t h2= std::hash<std::string>{}(s.last_name);return h1^(h2<<1);// or use boost::hash_combine}}; // Custom specialization of std::hash can be injected in namespace std.template<>struct std::hash<S>{std::size_t operator()(const S& s)constnoexcept{std::size_t h1= std::hash<std::string>{}(s.first_name);std::size_t h2= std::hash<std::string>{}(s.last_name);return h1^(h2<<1);// or use boost::hash_combine}}; int main(){std::string str="Meet the new boss...";std::size_t str_hash= std::hash<std::string>{}(str);std::cout<<"hash("<<std::quoted(str)<<") =\t"<< str_hash<<'\n';     S obj={"Hubert","Farnsworth"};// Using the standalone function object.std::cout<<"hash("<<std::quoted(obj.first_name)<<", "<<std::quoted(obj.last_name)<<") =\t"<< MyHash{}(obj)<<" (using MyHash) or\n\t\t\t\t"<< std::hash<S>{}(obj)<<" (using injected specialization)\n"; // Custom hash makes it possible to use custom types in unordered containers.// The example will use the injected std::hash<S> specialization above,// to use MyHash instead, pass it as a second template argument.std::unordered_set<S> names={obj,{"Bender","Rodriguez"},{"Turanga","Leela"}};for(autoconst& s: names)std::cout<<std::quoted(s.first_name)<<' '<<std::quoted(s.last_name)<<'\n';}

      Possible output:

      hash("Meet the new boss...") =  10656026664466977650hash("Hubert", "Farnsworth") =  12922914235676820612 (using MyHash) or                                12922914235676820612 (using injected specialization)"Bender" "Rodriguez""Turanga" "Leela""Hubert" "Farnsworth"

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2119C++11specializations for extended integer types were missingprovided
      LWG 2148C++11specializations for enumerations were missingprovided
      LWG 2543C++11std::hash might not be SFINAE-friendlymade SFINAE-friendly
      LWG 2817C++11specialization forstd::nullptr_t was missingprovided
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/hash&oldid=183394"

      [8]ページ先頭

      ©2009-2025 Movatter.jp