Last active
March 25, 2022 01:29
-
-
Save hjroh0315/fb9190608bddcd1dd380473d0760dc7b to your computer and use it in GitHub Desktop.
랜덤 시드를 활용한 해시 + 해시맵
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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