Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::hash<std::string_view>,std::hash<std::wstring_view>,std::hash<std::u8string_view>,std::hash<std::u16string_view>,std::hash<std::u32string_view>

      From cppreference.com
      <cpp‎ |string‎ |basic string view
       
       
       
      std::basic_string_view
       
      Defined in header<string_view>
      template<>struct hash<std::string_view>;
      (since C++17)
      template<>struct hash<std::wstring_view>;
      (since C++17)
      template<>struct hash<std::u8string_view>;
      (since C++20)
      template<>struct hash<std::u16string_view>;
      (since C++17)
      template<>struct hash<std::u32string_view>;
      (since C++17)

      Template specializations ofstd::hash for the various view classes for hashing views.

      These hashes equal the hashes of correspondingstd::basic_string classes: If S is one of the standard basic_string types, SV is the corresponding string view type, and s is an object of type S, thenstd::hash<S>()(s)==std::hash<SV>()(SV(s)).

      [edit]Example

      Run this code
      #include <iostream>#include <string_view>#include <unordered_set>usingnamespace std::literals; int main(){std::cout<<"\"A\"   #: "<<std::hash<std::string_view>{}("A"sv)<<'\n';std::cout<<"L\"B\"  #: "<<std::hash<std::wstring_view>{}(L"B"sv)<<'\n';std::cout<<"u8\"C\" #: "<<std::hash<std::u8string_view>{}(u8"C"sv)<<'\n';std::cout<<"u\"D\"  #: "<<std::hash<std::u16string_view>{}(u"D"sv)<<'\n';std::cout<<"U\"E\"  #: "<<std::hash<std::u32string_view>{}(U"E"sv)<<'\n'; // std::hash for string_view family makes it possible to keep these view-types// in unordered_* associative containers, such as unordered_set. But ensure// the lifespan of referenced strings is no less than lifespan of the container,// i.e. no dangling references occurred. std::unordered_set stars{"Rigel"sv,"Capella"sv,"Vega"sv,"Arcturus"sv}; for(std::string_viewconst& s: stars)std::cout<< s<<' ';std::cout<<'\n';}

      Possible output:

      "A"   #: 6919333181322027406L"B"  #: 11959850520494268278u8"C" #: 12432341034569643010u"D"  #: 312659256970442235U"E"  #: 18073225910249204957Arcturus Vega Capella Rigel

      [edit]See also

      (C++11)
      hash function object
      (class template)[edit]
      hash support for strings
      (class template specialization)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string_view/hash&oldid=167393"

      [8]ページ先頭

      ©2009-2025 Movatter.jp