c++ - using lambda do define hash function throws exception -
the next code defines unordered_set
. code compiles fine. using lambda function instead of functor throw when calling find:
libc++abi.dylib: terminate called throwing exception
#include <unordered_set> class pair_hash { public: size_t operator() (const std::pair<int, int> &x) const { homecoming std::hash<int>()(x.first) ^ std::hash<int>()(x.second); } }; int main() { std::unordered_set<std::pair<int, int>, pair_hash> temp; temp.find(std::make_pair(0,0)); std::function<std::size_t(std::pair<int , int>)> fpair_hash; fpair_hash = [](const std::pair<int, int>& v) -> std::size_t { homecoming std::hash<int>()(v.first) ^ std::hash<int>()(v.second); }; std::unordered_set<std::pair<int, int>, decltype(fpair_hash)> temp2; //why not work? temp2.find(std::make_pair(0,0)); homecoming 0; }
clang++ -std=c++11 -stdlib=libc++ -o test test.cpp
decltype(fpair_hash)
std::function<std::size_t(std::pair<int , int>)>
building set empty hash function.
you need provide function constructor of std::unordered_set
:
std::unordered_set<std::pair<int, int>, decltype(fpair_hash)> temp2(10, fpair_hash);
this should create work, using std::function
have overhead of polymorphic phone call , don't need it:
auto fpair_hash = [](const std::pair<int, int>& v) -> std::size_t { homecoming std::hash<int>()(v.first) ^ std::hash<int>()(v.second); };
finally, hash function not - maps pairs (x, x)
0
. perhaps using x * 17 + y * 13
instead of x ^ y
cut down probability of collisions.
c++ hash c++11 std unordered-set
No comments:
Post a Comment