Boost Hash Combine: Combining Hash Values in C++
The 'boost::hash_combine' function is provided by the Boost library and is used to combine multiple hash values into a single hash value. It's commonly used in hash functions to combine the hashes of different data members of a class or structure.
Here's an example of how to use 'boost::hash_combine':
#include <boost/functional/hash.hpp>
#include <iostream>
struct MyStruct {
int a;
float b;
std::string c;
};
// Define a hash function for MyStruct using boost::hash_combine
std::size_t hash_value(const MyStruct& s) {
std::size_t seed = 0;
boost::hash_combine(seed, s.a);
boost::hash_combine(seed, s.b);
boost::hash_combine(seed, s.c);
return seed;
}
int main() {
MyStruct s{42, 3.14f, "hello"};
std::size_t hash = hash_value(s);
std::cout << "Hash: " << hash << std::endl;
return 0;
}
In this example, the 'boost::hash_combine' function is used to combine the hash values of the 'a', 'b', and 'c' members of 'MyStruct'. The resulting hash value is then printed to the console.
原文地址: https://www.cveoy.top/t/topic/quBN 著作权归作者所有。请勿转载和采集!