Search
 
SCRIPT & CODE EXAMPLE
 

CPP

std::hash

#include <iostream>
#include <iomanip>
#include <functional>
#include <string>
#include <unordered_set>
 
struct S {
    std::string first_name;
    std::string last_name;
};
bool operator==(const S& lhs, const S& rhs) {
    return lhs.first_name == rhs.first_name && lhs.last_name == rhs.last_name;
}
 
// custom hash can be a standalone function object:
struct MyHash
{
    std::size_t operator()(S const& s) const noexcept
    {
        std::size_t h1 = std::hash<std::string>{}(s.first_name);
        std::size_t h2 = std::hash<std::string>{}(s.last_name);
        return h1 ^ (h2 << 1); // or use boost::hash_combine
    }
};
 
// custom specialization of std::hash can be injected in namespace std
template<>
struct std::hash<S>
{
    std::size_t operator()(S const& s) const noexcept
    {
        std::size_t h1 = std::hash<std::string>{}(s.first_name);
        std::size_t h2 = std::hash<std::string>{}(s.last_name);
        return h1 ^ (h2 << 1); // or use boost::hash_combine
    }
};
 
int main()
{
    std::string str = "Meet the new boss...";
    std::size_t str_hash = std::hash<std::string>{}(str);
    std::cout << "hash(" << std::quoted(str) << ") = " << str_hash << '
';
 
    S obj = { "Hubert", "Farnsworth" };
    // using the standalone function object
    std::cout << "hash(" << std::quoted(obj.first_name) << ", "
              << std::quoted(obj.last_name) << ") = "
              << MyHash{}(obj) << " (using MyHash)
" << std::setw(31) << "or "
              << std::hash<S>{}(obj) << " (using injected std::hash<S> specialization)
";
 
    // custom hash makes it possible to use custom types in unordered containers
    // The example will use the injected std::hash<S> specialization above,
    // to use MyHash instead, pass it as a second template argument
    std::unordered_set<S> names = {obj, {"Bender", "Rodriguez"}, {"Turanga", "Leela"} };
    for(auto& s: names)
        std::cout << std::quoted(s.first_name) << ' ' << std::quoted(s.last_name) << '
';
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: sinh to hop c++ 
Cpp :: Pawri Meme codechef solution in c++ 
Cpp :: The Three Topics codechef solution in c++ 
Cpp :: all in one c++ 
Cpp :: fabs in c++ example 
Cpp :: how to know how many numbers i deleted with erase command on multiset c++ 
Cpp :: draw point sfml 
Cpp :: bash script add another user 
Cpp :: c++ How to not use friend declaration when equipping a class with `operator<<` 
Cpp :: Studying Alphabet codechef solution in c++ 
Cpp :: std 
Cpp :: C++ Dynamic allocation failing 
Cpp :: Hiring Test codechef solution in c++ 
Cpp :: compile c++ program 
Cpp :: how to fixed how many digit will be after point in c++ 
Cpp :: C is widely used for systems-level software and embedded systems development. 
Cpp :: The iostream is the head er file which contains all the functions of program like cout, cin and etc. 
Cpp :: font family slick 
Cpp :: how to get the last digit of a number 
Cpp :: c++ how to print out 
Cpp :: cpp super 
Cpp :: powers of 2 in cpp 
Cpp :: c++ how to get maximum value 
Cpp :: c++ split string by sstream 
Cpp :: assignment operators 
Cpp :: call by value in c++ 
Cpp :: how to shorten code using using c++ in class with typename 
C :: terminal count files in directory 
C :: como programar a area de um triangulo em c 
C :: how to auto run something on cmd 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =