Espaços nominais
Variantes
Acções

std::hash

Da cppreference.com
< cpp‎ | utility


 
 
Biblioteca de utilitários
Digite apoio (basic types, RTTI, type traits)
Gerenciamento de memória dinâmica
De tratamento de erros
Utilidades do programa
Variadic funções
Data e hora
Objetos de função
hash
(C++11)
Os operadores relacionais
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
Pares e tuplas
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11)
Troque, avançar e avançar
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(C++11)
(C++11)
(C++11)
 
 
Definido no cabeçalho <functional>
template< class Key >
struct hash; // not defined
(desde C++11)
O modelo de hash define um objeto de função que implementa um função hash. Instâncias deste objeto de função definir um operator() que:
Original:
The hash template defines a function object that implements a função 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. Aceita um único parâmetro do 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. Retorna um valor de size_t tipo que representa o valor de hash do parâmetro.
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. Não lançar exceções ao chamado.
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. Para dois parâmetros k1 e k2 que são iguais, 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. Para dois parâmetros diferentes k1 e k2 que não são iguais, a probabilidade de que std::hash<Key>()(k1) == std::hash<Key>()(k2) deve ser muito pequeno, aproximando-se 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.
O modelo de hash é tanto CopyConstructible e 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.
O desordenada contêineres associativos std::unordered_set, std::unordered_multiset, std::unordered_map, std::unordered_multimap usar especializações da std::hash modelo como a função hash padrão.
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.

Índice

[editar] Tipos de membro

argument_type Key
result_type std::size_t

[editar] Funções de membro

constrói um objeto de função hash
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.

(função pública membro)
calcular o hash do argumento
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.

(função pública membro)

[editar] Especializações padrão para tipos básicos

Definido no cabeçalho <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*>;

[editar] Especializações padrão para tipos de biblioteca

apoio de hash para 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.

(especialização modelo. classe) [edit]
apoio de hash para 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.

(especialização modelo. classe) [edit]
apoio de hash para 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.

(especialização modelo. classe) [edit]
apoio de hash para 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.

(especialização modelo. classe) [edit]
apoio de hash para std::shared_ptr
Original:
hash support for std::shared_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(especialização modelo. classe) [edit]
apoio de hash para 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.

(especialização modelo. classe) [edit]
apoio de hash para 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.

(especialização modelo. classe)
apoio de hash para 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.

(especialização modelo. classe)

[editar] Exemplos

Demonstra o cálculo de um hash para std::string, um tipo que já tem uma especialização de 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';
}

Saída:

391070135

Demonstra a criação de uma função hash para um tipo definido pelo usuário. Usando isso como um parâmetro de modelo para std::unordered_map, std::unordered_set, etc também exige especialização de 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";
 
}

Saída:

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

Demonstra como se especializar std::hash para um tipo definido pelo usuário .

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";
}

Saída:

hash(s) = 32902390710