Category : hash

I have this very simple POD struct with 3 shorts in it, and I’m planning to use it hundreds of millions of times in a unordered_set. Here’s is what I’m doing so far: struct ps3 { short v1; short v2; short v3; bool operator==(const ps3& other) const { return v1 == other.v1 && v2 == ..

Read more

As pointed out in answers to Generic hash for tuples in unordered_map and elsewhere, Boost has an implementation of boost::hash<tuple<string, string>>{}(). So we can do things like: #include <boost/functional/hash.hpp> #include <boost/unordered_map.hpp> std::tuple<string, string> t; size_t bh = boost::hash<tuple<string, string>>{}(t); boost::unordered_map<tuple<string, string>, int> bm; but not size_t sh = std::hash<tuple<string, string>>{}(t); // error C2338: The C++ ..

Read more

i have to return the max len of consecutive seq present in an array. consider the example:- N = 7 a[] = {2,6,1,9,4,5,3} my code should return 6 but its giving 1. don’t know how? int findLongestConseqSubseq(int arr[], int N) { //Your code here unordered_map<int,int> mp; int ans=0; for(int i=0;i<N;i++){ if(mp.count(arr[i])>0){ continue; } int len1=mp[arr[i]-1]; ..

Read more

To construct an unordered map with a customized hash function struct EnumClassHash { template <typename T> std::size_t operator()(T t) const { return static_cast<std::size_t>(t); } }; enum class MyEnum {}; std::unordered_map<MyEnum, int, EnumClassHash> myMap; To construct a for_each function struct Class { void operator() (int a) { cout << a * 3 << " "; } ..

Read more