Better implementation using template .

Revision en5, by arjun17, 2018-08-19 13:58:27
#include <bits/stdc++.h>
using namespace std;

const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
/*const long long RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();*/


struct HASH {
    template <class T>
    T operator()(T x) const { 
        return hash<T>{}(x ^ RANDOM ); 
    }
};

int main(int argc, char const *argv[])
{
    unordered_map<int, int, HASH> table;
    /* unordered_map<long long, int, HASH> table; */

    return 0;
}

I'm implementing own hash function in unordered_map and facing a problem.

If my map key is int type I've to write const int RANDOM

If my map key is long long type I've to write const long long RANDOM

blah blah ......

How it can be better using Template?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English arjun17 2018-08-19 13:58:27 13
en4 English arjun17 2018-08-19 13:56:53 30
en3 English arjun17 2018-08-19 13:54:52 77
en2 English arjun17 2018-08-19 13:53:33 8
en1 English arjun17 2018-08-19 13:52:45 779 Initial revision (published)