-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaps.cpp
More file actions
83 lines (68 loc) · 1.68 KB
/
Maps.cpp
File metadata and controls
83 lines (68 loc) · 1.68 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* \file Maps.cpp
* \brief http://thispointer.com/map-vs-unordered_map-when-to-choose-one-over-another/
*
* \todo
*/
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
void
testMap()
{
/**
* std::map
*
* Internally store elements in a balanced BST.
* Therefore, elements will be stored in sorted order of keys.
*/
std::cout << "\n" << __FUNCTION__ << std::endl;
std::map<int, int> values;
values[5] = 10;
values[3] = 5;
values[20] = 100;
values[1] = 1;
for (auto &it_value : values) {
std::cout << it_value.first << " : " << it_value.second << '\n';
}
}
//-------------------------------------------------------------------------------------------------
void
testUnorderedMap()
{
/**
* std::unordered_map
*
* Store elements using hash table.
* Therefore, elements will not be stored in any sorted order. They will be stored in arbitrary order .
*/
std::cout << "\n" << __FUNCTION__ << std::endl;
std::unordered_map<int, int> values;
values[5] = 10;
values[3] = 5;
values[20] = 100;
values[1] = 1;
for (auto &it_value : values) {
std::cout << it_value.first << " : " << it_value.second << '\n';
}
}
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
testMap();
testUnorderedMap();
return 0;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
testMap
1 : 1
3 : 5
5 : 10
20 : 100
testUnorderedMap
1 : 1
20 : 100
5 : 10
3 : 5
#endif