-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainmap.cpp
107 lines (83 loc) · 2.81 KB
/
mainmap.cpp
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
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
int main()
{
map<string, string> cacheMap;
cacheMap.insert(pair<string, string>("query 1 2", "123"));
map<string, string>::iterator itr;
cout << "\nThe map cacheMap is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = cacheMap.begin(); itr != cacheMap.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
int main001()
{
// empty map container
map<int, int> cacheMap;
// insert elements in random order
cacheMap.insert(pair<int, int>(1, 40));
cacheMap.insert(pair<int, int>(2, 30));
cacheMap.insert(pair<int, int>(3, 60));
cacheMap.insert(pair<int, int>(4, 20));
cacheMap.insert(pair<int, int>(5, 50));
cacheMap.insert(pair<int, int>(6, 50));
cacheMap.insert(pair<int, int>(7, 10));
// printing map cacheMap
map<int, int>::iterator itr;
cout << "\nThe map cacheMap is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = cacheMap.begin(); itr != cacheMap.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// assigning the elements from cacheMap to gquiz2
map<int, int> gquiz2(cacheMap.begin(), cacheMap.end());
// print all elements of the map gquiz2
cout << "\nThe map gquiz2 after"
<< " assign from cacheMap is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// remove all elements up to
// element with key=3 in gquiz2
cout << "\ngquiz2 after removal of"
" elements less than key=3 : \n";
cout << "\tKEY\tELEMENT\n";
gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
// remove all elements with key = 4
int num;
num = gquiz2.erase(4);
cout << "\ngquiz2.erase(4) : ";
cout << num << " removed \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// lower bound and upper bound for map cacheMap key = 5
cout << "cacheMap.lower_bound(5) : "
<< "\tKEY = ";
cout << cacheMap.lower_bound(5)->first << '\t';
cout << "\tELEMENT = "
<< cacheMap.lower_bound(5)->second << endl;
cout << "cacheMap.upper_bound(5) : "
<< "\tKEY = ";
cout << cacheMap.upper_bound(5)->first << '\t';
cout << "\tELEMENT = "
<< cacheMap.upper_bound(5)->second << endl;
return 0;
}