Skip to content

Instantly share code, notes, and snippets.

@hjroh0315
Last active March 25, 2022 01:29
Show Gist options
  • Select an option

  • Save hjroh0315/fb9190608bddcd1dd380473d0760dc7b to your computer and use it in GitHub Desktop.

Select an option

Save hjroh0315/fb9190608bddcd1dd380473d0760dc7b to your computer and use it in GitHub Desktop.
랜덤 시드를 활용한 해시 + 해시맵
#include<bits/stdc++.h>
using namespace std;
struct SingletonRand
{
size_t value;
SingletonRand()
{
auto curTime = chrono::system_clock::now(); auto duration = curTime.time_since_epoch();
auto timer = chrono::duration_cast<chrono::nanoseconds>(duration).count();
mt19937_64 mtRand(timer);
value=mtRand();
}
};
SingletonRand Seed = SingletonRand();
template <class T>
struct RandHasher
{
size_t operator()(T const& rh) const
{
return hash<size_t>()(Seed.value)^hash<T>()(rh);
}
};
int main()
{
unordered_map<string,int,RandHasher<string>> umap;
umap["hello"] = 12345;
umap["world"] = 67890;
cout << umap["hello"] << " " << umap["world"] << endl;
cout<< RandHasher<string>()("hello") << " " << RandHasher<string>()("world") << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment