Namensräume
Varianten

std::hash

Aus cppreference.com
< cpp‎ | utility


definiert in Header <functional>
template<class Key >
struct hash;// not defined
(seit C++11)
Das Hash-Template definiert ein Funktionsobjekt, das eine Hash-Funktion implementiert. Instanzen dieses Funktionsobjekts definieren einen operator() der:
Original:
The hash template defines a function object that implements a Hash-Funktion. Instances of this function object define an operator() that:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1. Einen einzelnen Parameter des Typs Key entgegennimmt.
Original:
1. Accepts a single parameter of type Key.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2. Einen Wert vom Typ size_t zurückgibt, der den Hash-Wert des Parameters darstellt.
Original:
2. Returns a value of type size_t that represents the hash value of the parameter.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3. Während der Ausführung keine Exception auslösen.
Original:
3. Does not throw exceptions when called.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4. Für zwei identische Parameter k1 und k2 gilt: std::hash<Key>()(k1)== std::hash<Key>()(k2) .
Original:
4. For two parameters k1 and k2 that are equal, std::hash<Key>()(k1)== std::hash<Key>()(k2).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5. Für zwei unterschiedliche Parameter k1 und k2 sollte die Wahrscheinlichkeit, dass std::hash<Key>()(k1)== std::hash<Key>()(k2) minimal sein, näherungsweise 1.0/std::numeric_limits<size_t>::max() .
Original:
5. For two different parameters k1 and k2 that are not equal, the probability that std::hash<Key>()(k1)== std::hash<Key>()(k2) should be very small, approaching 1.0/std::numeric_limits<size_t>::max().
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Das Hash-Template ist erfüllt die Konzepte CopyConstructible und Destructible.
Original:
The hash template is both CopyConstructible and Destructible.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Die ungeordnete assoziative Container std::unordered_set, std::unordered_multiset, std::unordered_map, std::unordered_multimap verwenden Spezialisierungen der Vorlage std::hash als Standard-Hash-Funktion .
Original:
The unordered associative containers std::unordered_set, std::unordered_multiset, std::unordered_map, std::unordered_multimap use specializations of the template std::hash as the default hash function.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Inhaltsverzeichnis

[Bearbeiten]Mitglied Typen

argument_typeKey
result_typestd::size_t

[Bearbeiten]Member-Funktionen

konstruiert eine Hash-Funktion Objekt
Original:
constructs a hash function object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(öffentliche Elementfunktion)
Berechnung der Hash-Wert des Arguments
Original:
calculate the hash of the argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(öffentliche Elementfunktion)

[Bearbeiten]Standard Spezialisierungen für Grundtypen

definiert in Header <functional>
template<>struct hash<bool>;

template<>struct hash<char>;
template<>struct hash<signedchar>;
template<>struct hash<unsignedchar>;
template<>struct hash<char16_t>;
template<>struct hash<char32_t>;
template<>struct hash<wchar_t>;
template<>struct hash<short>;
template<>struct hash<unsignedshort>;
template<>struct hash<int>;
template<>struct hash<unsignedint>;
template<>struct hash<long>;
template<>struct hash<longlong>;
template<>struct hash<unsignedlong>;
template<>struct hash<unsignedlonglong>;
template<>struct hash<float>;
template<>struct hash<double>;
template<>struct hash<longdouble>;

template<class T >struct hash<T*>;

[Bearbeiten]Standard Spezialisierungen für Bibliothekstypen

Hash-Unterstützung für Strings
Original:
hash support for strings
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(class Template-Spezialisierung)[edit]
Hash-Unterstützung für std::error_code
Original:
hash support for std::error_code
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(class Template-Spezialisierung)[edit]
Hash-Unterstützung für std::bitset
Original:
hash support for std::bitset
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(class Template-Spezialisierung)[edit]
Hash-Unterstützung für std::unique_ptr
(class Template-Spezialisierung)[edit]
Hash-Unterstützung für std::shared_ptr
(class Template-Spezialisierung)[edit]
Hash-Unterstützung für std::type_index
Original:
hash support for std::type_index
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(class Template-Spezialisierung)[edit]
Hash-Unterstützung für std::vector<bool>
Original:
hash support for std::vector<bool>
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(class Template-Spezialisierung)
Hash-Unterstützung für std::thread::id
Original:
hash support for std::thread::id
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(class Template-Spezialisierung)

[Bearbeiten]Beispiele

Demonstriert die Berechnung eines Hash für std::string, ein Typ, der bereits über eine Hash-Spezialisierung .

Original:

Demonstrates the computation of a hash for std::string, a type that already has a hash specialization.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>#include <functional>#include <string>   int main(){std::string str ="Meet the new boss..."; std::hash<std::string> hash_fn; size_t str_hash = hash_fn(str);   std::cout<< str_hash <<'\n';}

Output:

391070135

Veranschaulicht Schaffung einer Hash-Funktion für einen Benutzer definierten Typ. Mit diesem als Template-Parameter für std::unordered_map, std::unordered_set, etc. erfordert auch Spezialisierung std::equal_to .

Original:

Demonstrates creation of a hash function for a user defined type. Using this as a template parameter for std::unordered_map, std::unordered_set, etc. also requires specialization of std::equal_to.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>#include <functional>#include <string>   struct S {std::string first_name;std::string last_name;};   template<class T>class MyHash;   template<>class MyHash<S>{public: size_t operator()(const S &s)const{ size_t h1 = std::hash<std::string>()(s.first_name); size_t h2 = std::hash<std::string>()(s.last_name);return h1 ^(h2 <<1);}};   int main(){std::string s1 ="Hubert";std::string s2 ="Farnsworth"; std::hash<std::string> h1;   S n1; n1.first_name= s1; n1.last_name= s2;   std::cout<<"hash(s1) = "<< h1(s1)<<"\n"<<"hash(s2) = "<< std::hash<std::string>()(s2)<<"\n"<<"hash(n1) = "<< MyHash<S>()(n1)<<"\n";   }

Output:

hash(s1) = 6119893563398376542 hash(s2) = 14988020022735710972 hash(n1) = 17649170831080298918

Veranschaulicht, wie std::hash für einen benutzerdefinierten Typ spezialisieren .

Original:

Demonstrates how to specialize std::hash for a user defined type.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>#include <functional>#include <string>   struct S {std::string first_name;std::string last_name;};   namespace std {template<>class hash<S>{public: size_t operator()(const S &s)const{ size_t h1 = std::hash<std::string>()(s.first_name); size_t h2 = std::hash<std::string>()(s.last_name);return h1 ^( h2 <<1);}};}   int main(){ S s; s.first_name="Bender"; s.last_name="Rodriguez"; std::hash<S> hash_fn;   std::cout<<"hash(s) = "<< hash_fn(s)<<"\n";}

Output:

hash(s) = 32902390710
close