-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-containers.cc
64 lines (55 loc) · 1.69 KB
/
06-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
//
// Program
// Containers: std::unordered_map, std::unordered_multimap usage
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o 06-containers 06-containers.cc
//
// Execution
// ./06-containers
//
#include <iostream>
#include <unordered_map>
// Print map
template <typename UserMap>
static void print_map(const UserMap& m) {
for (const auto& item: m) {
std::cout << item.first << " : " << item.second << '\n';
}
}
//
// Entry function
//
int main() {
// std::unordered_map declaration
std::cout << "std::unordered_map ---" << '\n';
{
// Same as std::unordered_map but it will be unordered
std::unordered_map<int, std::string> m;
m.insert(std::make_pair(3, "entry3"));
m.insert(std::make_pair(1, "entry1"));
m.insert(std::make_pair(2, "entry2"));
m.insert(std::make_pair(6, "entry6"));
m.insert(std::make_pair(4, "entry4"));
m.insert(std::make_pair(5, "entry5"));
m.insert(std::make_pair(3, "entry3_2")); // it will fail
std::cout << "Show items of the std::unordered_map " << m.size() << '\n';
print_map(m);
}
// std::unordered_multimap declaration
std::cout << "std::unordered_multimap ---" << '\n';
{
// Same as std::unordered_multimap but it will be unordered
std::unordered_multimap<int, std::string> m;
m.insert(std::make_pair(3, "entry3"));
m.insert(std::make_pair(1, "entry1"));
m.insert(std::make_pair(2, "entry2"));
m.insert(std::make_pair(6, "entry6"));
m.insert(std::make_pair(4, "entry4"));
m.insert(std::make_pair(5, "entry5"));
m.insert(std::make_pair(3, "entry3_2"));
std::cout << "Show items of the std::unordered_multimap " << m.size() << '\n';
print_map(m);
}
return 0;
}