文章目录
一、unordered_map
template < class Key, // unordered_map::key_type
class T, // unordered_map::mapped_type
class Hash = hash<Key>, // unordered_map::hasher
class Pred = equal_to<Key>, // unordered_map::key_equal
class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type
> class unordered_map;
1、常用成员函数
2、案例
#include<iostream>
#include <unordered_map>
#include <string>
using namespace std;
unordered_map<int, string> u_map = {{1, "jj"}, {2, "pp"}, {3, "ii"}};
void test() {
// ret: 3
//cout << "size: " << u_map.size() << endl;
// ret: 357913941
//cout << "max_size: " << u_map.max_size() << endl;
// ret: find!!! pp
//auto it = u_map.find(2);
//if(it != u_map.end())
// cout << "find!!! " << it->second << endl;
//else
// cout << "no find!!!" << endl;
// ret: find 2"
//if(u_map.count(2) == 0) {
// cout << "2 not in u_set" << endl;
//}else
// cout << "find 2" << endl;
// ret: 2 pp
//auto it = u_map.equal_range(2);
//cout << it.first->first << " " << it.first->second << endl;
// ret: 4
//u_map.emplace(100, "uu");
//cout << "size: " << u_map.size() << endl;
// ret: 4
//u_map.emplace_hint(u_map.begin(), 200, "kk");
//cout << "size: " << u_map.size() << endl;
// ret: 4
//u_map.insert({290, "ll"});
//cout << "size: " << u_map.size() << endl;
// ret: 0
//u_map.clear();
//cout << "size: " << u_map.size() << endl;
// ret: 3 ii 1 jj
//u_map.erase(2);
//for(auto i:u_map) {
// cout << i.first << " " << i.second << " ";
//}
// ret: 20 ss 19 yy
//unordered_map<int, string> tmp = {{19, "yy"}, {20, "ss"}};
//u_map.swap(tmp);
//for(auto i:u_map) {
// cout << i.first << " " << i.second << " ";
//}
// ret: 7
//cout << u_map.bucket_count() << endl;
// ret: 357913941
//cout << u_map.max_bucket_count() << endl;
/* ret:
0 0
1 1
2 1
3 1
4 0
5 0
6 0
*/
//for(size_t i=0; i<u_map.bucket_count(); ++i) {
// cout << i << " " << u_map.bucket_size(i) << endl;
//}
/* ret:
3 in #3
2 in #2
1 in #1
*/
//for(auto i:u_map) {
// cout << i.first << " in #" << u_map.bucket(i.first) << endl;
//}
/* ret:
负载因子:0.428571
最大负载因子:1
*/
//cout << "负载因子:" << u_map.load_factor() << endl;
//cout << "最大负载因子:" << u_map.max_load_factor() << endl;
// ret: 11
//u_map.rehash(8);
//cout << u_map.bucket_count() << endl;
// ret: 11
//u_map.reserve(8);
//cout << u_map.bucket_count() << endl;
}
int main() {
test();
return 0;
}