-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathunordered_multiset
More file actions
61 lines (46 loc) · 1.43 KB
/
unordered_multiset
File metadata and controls
61 lines (46 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* *********************************************************
* KNOWLEDGE CENTER
* st::unordered_multiset
* Detailed Video Explanation: https://youtu.be/AdCryKn9T9o
********************************************************** */
#include <iostream>
#include <functional>
#include <unordered_set>
using namespace std;
class Student{
public:
int id;
string name;
bool operator==(const Student& s) const{
return (this->id == s.id && this->name == s.name);
}
void print_student() const {
cout << "[ id = " << id << ", name = " << name << "]\n";
}
};
class StudentHashFunction{
public:
size_t operator()(const Student& s) const{
return (hash<int>{}(s.id) + hash<string>{}(s.name));
}
};
int main(){
unordered_multiset<int> us = {5, 10, 4, 20, 5, 5, 15};
for(int x: us)
cout << x << " ";
cout << endl;
auto its = us.equal_range(5);
for(auto it = its.first; it != its.second; ++it)
cout << *it << " ";
cout << endl;
cout << "size = " << us.size() << endl;
cout << "count(5) = " << us.count(5) << endl;
cout << "num erased(5) " << us.erase(5) << endl;
cout << boolalpha << "found 16 = " << (us.find(16) != us.end()) << endl;
cout << "num buckets = " << us.bucket_count() << endl;
cout << "load factor = " << us.load_factor() << endl;
unordered_multiset<Student, StudentHashFunction> uss = {{50, "Simon"}, {20, "Thomas"}, {50, "Simon"}};
for(auto& st: uss)
st.print_student();
return 0;
}