-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-containers.cc
95 lines (83 loc) · 2.49 KB
/
07-containers.cc
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Program
// Containers: std::unordered_map with enum class
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o 07-containers 07-containers.cc
//
// Execution
// ./07-containers
//
#include <iostream>
#include <functional>
#include <unordered_map>
enum class Hardware {
TARGET_A = 0,
TARGET_B,
TARGET_C,
TARGET_D,
TARGET_E,
};
// Print map
template <typename UserMap>
static void print_map(const UserMap& m) {
for (const auto& item: m) {
std::cout << static_cast<int>(item.first) << " : " << item.second << '\n';
}
}
//
// Entry function
//
int main() {
// std::unordered_map declaration
std::cout << "std::unordered_map with enum_class ---" << '\n';
{
// Same as std::unordered_map but it will be unordered
std::unordered_map<Hardware, std::string> m;
m.insert(std::make_pair(Hardware::TARGET_A, "target1"));
m.insert(std::make_pair(Hardware::TARGET_B, "target2"));
m.insert(std::make_pair(Hardware::TARGET_C, "target3"));
m.insert(std::make_pair(Hardware::TARGET_D, "target4"));
m.insert(std::make_pair(Hardware::TARGET_E, "target5"));
std::cout << "Show items of the std::unordered_map " << m.size() << '\n';
print_map(m);
}
std::cout << "std::unordered_map with enum_class (custom hash function) ---" << '\n';
{
struct KeyHash {
std::size_t operator()(const Hardware& key) const {
return std::hash<std::size_t>{}(static_cast<std::size_t>(key));
}
};
struct KeyEqual {
size_t operator()(const Hardware& lhs, const Hardware& rhs) const {
return static_cast<size_t>(lhs) == static_cast<size_t>(rhs);
}
};
// Same as std::unordered_map but it will be unordered
std::unordered_map<Hardware, std::string, KeyHash, KeyEqual> m;
m.insert(std::make_pair(Hardware::TARGET_A, "target1"));
m.insert(std::make_pair(Hardware::TARGET_B, "target2"));
m.insert(std::make_pair(Hardware::TARGET_C, "target3"));
m.insert(std::make_pair(Hardware::TARGET_D, "target4"));
m.insert(std::make_pair(Hardware::TARGET_E, "target5"));
std::cout << "Show items of the std::unordered_map " << m.size() << '\n';
print_map(m);
}
return 0;
}
// Output
// std::unordered_map with enum_class ---
// Show items of the std::unordered_map 5
// 4 : target5
// 3 : target4
// 2 : target3
// 1 : target2
// 0 : target1
// std::unordered_map with enum_class (custom hash function) ---
// Show items of the std::unordered_map 5
// 4 : target5
// 3 : target4
// 2 : target3
// 1 : target2
// 0 : target1