std::hash - cppreference.com
Da cppreference.com.
|
|
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
<tbody> </tbody>
| Elemento definito nell'header <functional> |
||
|
|
(dal C++11) | |
Il modello hash definisce un oggetto che implementa una funzione funzione di hash. Le istanze di questo oggetto funzione di definire un operator() che:
Original:
The hash template defines a function object that implements a funzione di hash. 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. Accetta un singolo parametro di tipo Key.
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. Restituisce un valore di size_t tipo che rappresenta il valore hash del parametro.
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. Non generare eccezioni quando viene chiamato.
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. Per due parametri k1 e k2 che sono uguali, 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. Per due parametri diversi k1 e k2 che non sono uguali, la probabilità che std::hash<Key>()(k1) == std::hash<Key>()(k2) dovrebbe essere molto piccolo, avvicinando 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.
Il modello di hash è sia CopyConstructible e Destructible.
Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
L'ordinata contenitori associativi std::unordered_set, std::unordered_multiset, std::unordered_map, std::unordered_multimap utilizzare specializzazioni della std::hash modello come la funzione di hash predefinito.
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.
Membri tipi
argument_type
|
Key
|
result_type
|
std::size_t |
Membri funzioni
costruisce un oggetto funzione hash Original: constructs a hash function object The text has been machine-translated via Google Translate. (metodo pubblico) | |
calcolare l'hash dell'argomento Original: calculate the hash of the argument The text has been machine-translated via Google Translate. (metodo pubblico) | |
Specializzazioni standard per tipi di base
<tbody> </tbody>
| Elemento definito nell'header <functional> |
||
|
|
||
Specializzazioni standard per i tipi di libreria
(C++11) |
supporto per l'hash di stringhe (classe modello di specializzazione) [modifica] |
(C++11) |
sostegno hash per std::error_code Original: hash support for std::error_code The text has been machine-translated via Google Translate. (classe modello di specializzazione) [modifica] |
(C++11) |
sostegno hash per std::bitset Original: hash support for std::bitset The text has been machine-translated via Google Translate. (classe modello di specializzazione) [modifica] |
(C++11) |
sostegno hash per std::unique_ptr Original: hash support for std::unique_ptr The text has been machine-translated via Google Translate. (classe modello di specializzazione) [modifica] |
(C++11) |
sostegno hash per std::shared_ptr Original: hash support for std::shared_ptr The text has been machine-translated via Google Translate. (classe modello di specializzazione) [modifica] |
(C++11) |
sostegno hash per std::type_index Original: hash support for std::type_index The text has been machine-translated via Google Translate. (classe modello di specializzazione) [modifica] |
(C++11) |
sostegno hash per Original: hash support for The text has been machine-translated via Google Translate. (classe modello di specializzazione) |
(C++11) |
sostegno hash per std::thread::id Original: hash support for std::thread::id The text has been machine-translated via Google Translate. (classe modello di specializzazione) |
Esempi
Viene illustrato il calcolo di un hash per std::string, un tipo che ha già una specializzazione hash .
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:
Viene illustrata la creazione di una funzione hash per un tipo definito dall'utente. Usando questo come parametro di template per std::unordered_map, std::unordered_set, ecc richiede anche la specializzazione di 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
Viene illustrato come specializzarsi std::hash di un tipo definito dall'utente .
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: