std::hash
[] |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Dclar dans l'en-tte <functional>
|
||
template< class Key > struct hash; // not defined |
(depuis C++11) | |
operator() que:operator() that:You can help to correct and verify the translation. Click here for instructions.
KeyKey.You can help to correct and verify the translation. Click here for instructions.
size_t type qui reprsente la valeur de hachage du paramtre .size_t that represents the hash value of the parameter.You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
k1 et k2 qui sont gaux, std::hash<Key>()(k1) == std::hash<Key>()(k2) .k1 and k2 that are equal, std::hash<Key>()(k1) == std::hash<Key>()(k2).You can help to correct and verify the translation. Click here for instructions.
k1 et k2 qui ne sont pas gales, la probabilit que std::hash<Key>()(k1) == std::hash<Key>()(k2) devrait tre trs faible, se rapprochant 1.0/std::numeric_limits<size_t>::max() .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().You can help to correct and verify the translation. Click here for instructions.
CopyConstructible et Destructible .CopyConstructible and Destructible.You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Types de membres
argument_type
|
Key
|
result_type
|
std::size_t |
Fonctions membres
construit un objet fonction de hachage 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. (fonction membre publique) | |
calculer l'empreinte de l'argument 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. (fonction membre publique) | |
Spcialisations standard pour les types de base
<tbody> </tbody>| Dclar dans l'en-tte <functional>
|
||
template<> struct hash<bool>; template<> struct hash<char>; template<> struct hash<signed char>; template<> struct hash<unsigned char>; template<> struct hash<char16_t>; template<> struct hash<char32_t>; template<> struct hash<wchar_t>; template<> struct hash<short>; template<> struct hash<unsigned short>; template<> struct hash<int>; template<> struct hash<unsigned int>; template<> struct hash<long>; template<> struct hash<long long>; template<> struct hash<unsigned long>; template<> struct hash<unsigned long long>; template<> struct hash<float>; template<> struct hash<double>; template<> struct hash<long double>; template< class T > struct hash<T*>; |
||
Spcialisations standard pour les types de bibliothques
(C++11) (C++11) (C++11) (C++11) |
soutien de hachage pour chaines de caractres 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. (classe gnrique spcialise) |
(C++11) |
soutien de hachage pour 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. (classe gnrique spcialise) |
(C++11) |
soutien de hachage pour 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. (classe gnrique spcialise) |
(C++11) |
soutien de hachage pour std::unique_ptr Original: hash support for std::unique_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (classe gnrique spcialise) |
(C++11) |
Support du hachage pour std::shared_ptr (classe gnrique spcialise) |
(C++11) |
soutien de hachage pour 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. (classe gnrique spcialise) |
(C++11) |
soutien de hachage pour 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. (classe gnrique spcialise) |
(C++11) |
soutien de hachage pour 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. (classe gnrique spcialise) |
Exemples
Dmontre le calcul de hachage pour un std::string, un type qui a dj une spcialisation de hachage .
Demonstrates the computation of a hash for std::string, a type that already has a hash specialization.
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';
}
Rsultat :
391070135
Dmontre cration d'une fonction de hachage pour un type dfini par l'utilisateur. L'utilisation de ce paramtre comme un modle pour std::unordered_map, std::unordered_set, etc exige galement la spcialisation des std::equal_to .
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.
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";
}
Rsultat :
hash(s1) = 6119893563398376542
hash(s2) = 14988020022735710972
hash(n1) = 17649170831080298918
Montre comment se spcialiser std::hash pour un type dfini par l'utilisateur .
Demonstrates how to specialize std::hash for a user defined type.
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";
}
Rsultat :
hash(s) = 32902390710