STL-unordered
This commit is contained in:
40
STL/STL-unordered_map/unordered_map.cpp
Normal file
40
STL/STL-unordered_map/unordered_map.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include <unordered_map>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class hashdef{
|
||||||
|
public:
|
||||||
|
//template<class T><3E><><EFBFBD><EFBFBD>̫<EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>string<6E><67>
|
||||||
|
size_t operator()(const string& a) const{//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>const<73><74><EFBFBD><EFBFBD>hash<73>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>䵱const<73><74><EFBFBD><EFBFBD>
|
||||||
|
//ͬ<><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>static<69><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>õ<EFBFBD>this
|
||||||
|
int hash;
|
||||||
|
int seed = 114514;
|
||||||
|
for(char x : a){
|
||||||
|
hash += static_cast<int>(x)*31 + seed;
|
||||||
|
hash %= 1009;
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
int main(){
|
||||||
|
hashdef a;
|
||||||
|
unordered_map<string, string, hashdef> m1{make_pair("1","111")};
|
||||||
|
unordered_map<string, string> m2;
|
||||||
|
m1.insert({make_pair("2","222"),make_pair("3","333"),make_pair("6","666"),make_pair("5","555")});
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȳ鿴<C8B2><E9BFB4>ϣֵ
|
||||||
|
cout << a("1") << " " << a("2") << " " << a("3") << " " << a("5") << " " << a("6") << endl;
|
||||||
|
m1.rehash(1000);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͱ<EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>Ϊ1009<30><39><EFBFBD><EFBFBD><EFBFBD><EFBFBD>hash(<28><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>hash)
|
||||||
|
m2.max_load_factor(0.7f);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>=Ԫ<>ظ<EFBFBD><D8B8><EFBFBD>/Ͱ<><CDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD>ÿ<EFBFBD><C3BF>Ͱ<EFBFBD><CDB0><EFBFBD><EFBFBD>0.7<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
||||||
|
cout << "bucket_count: " << m1.bucket_count() << endl;//hashtable_policy.h<>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>n<EFBFBD><6E><EFBFBD><EFBFBD>
|
||||||
|
cout << m1.max_load_factor() << " " << m1.load_factor() << endl;
|
||||||
|
m1["7"] = "777";
|
||||||
|
cout << m1.at("6") << " " << m1["5"] << " " << m1.find("6")->second << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
1007 29 60 122 153
|
||||||
|
bucket_count: 1031
|
||||||
|
1 0.00484966
|
||||||
|
666 555 666
|
||||||
|
*/
|
20
STL/STL-unordered_map/unordered_multimap.cpp
Normal file
20
STL/STL-unordered_map/unordered_multimap.cpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
//<2F>÷<EFBFBD><C3B7><EFBFBD>unordered_map - multimapһ<70><D2BB>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
int main(){
|
||||||
|
unordered_multimap<int, int> m;
|
||||||
|
m.insert({make_pair(1,111), make_pair(2,222), make_pair(3,333), make_pair(8,888), make_pair(6,666), make_pair(5,555)});
|
||||||
|
m.insert({make_pair(1,11), make_pair(2,22), make_pair(1,1)});
|
||||||
|
cout << m.count(1) << " " << m.find(1)->first << "," << m.find(1)->second << endl;
|
||||||
|
m.erase(m.find(1));
|
||||||
|
cout << m.count(1) << " " << m.find(1)->first << "," << m.find(1)->second << endl;
|
||||||
|
m.erase(1);
|
||||||
|
cout << m.count(1) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
3 1,1
|
||||||
|
2 1,11
|
||||||
|
0
|
||||||
|
*/
|
Reference in New Issue
Block a user