Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::collate<CharT>::hash,std::collate<CharT>::do_hash

      From cppreference.com
      <cpp‎ |locale‎ |collate
       
       
       
      Localization library
       
       
      Defined in header<locale>
      public:
      long hash(const CharT* beg,const CharT* end)const;
      (1)
      protected:
      virtuallong do_hash(const CharT* beg,const CharT* end)const;
      (2)
      1) Public member function, calls the protected virtual member functiondo_hash of the most derived class.
      2) Converts the character sequence[begend) to an integer value that is equal to the hash obtained for all strings that collate equivalent in this locale (compare() returns0). For two strings that do not collate equivalent, the probability that their hashes are equal should be very small, approaching1.0/std::numeric_limits<unsignedlong>::max().

      Contents

      [edit]Parameters

      beg - pointer to the first character in the sequence to hash
      end - one past the end pointer for the sequence to hash

      [edit]Return value

      The hash value that respects collation order.

      [edit]Note

      The system-supplied locales normally do not collate two strings as equivalent (compare() does not return0) ifbasic_string::operator== returnsfalse, but a user-installedstd::collate facet may provide different collation rules, for example, it may treat strings as equivalent if they have the same Unicode normalized form.

      [edit]Example

      Demonstrates a locale-aware unordered container.

      Run this code
      #include <iostream>#include <locale>#include <string>#include <unordered_set> struct CollateHash{template<typename CharT>long operator()(conststd::basic_string<CharT>& s)const{returnstd::use_facet<std::collate<CharT>>(std::locale()).hash(&s[0],&s[0]+ s.size());}};struct CollateEq{template<typename CharT>bool operator()(conststd::basic_string<CharT>& s1,conststd::basic_string<CharT>& s2)const{returnstd::use_facet<std::collate<CharT>>(std::locale()).compare(&s1[0],&s1[0]+ s1.size(),&s2[0],&s2[0]+ s2.size())==0;}}; int main(){std::locale::global(std::locale("en_US.utf8"));std::wcout.imbue(std::locale()); std::unordered_set<std::wstring, CollateHash, CollateEq> s2={L"Foo", L"Bar"};for(auto& str: s2)std::wcout<< str<<' ';std::cout<<'\n';}

      Possible output:

      Bar Foo

      [edit]See also

      hash support for strings
      (class template specialization)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/collate/hash&oldid=160060"

      [8]ページ先頭

      ©2009-2025 Movatter.jp