Repository navigation
Expand file tree
/
Copy pathmap.cpp
More file actions
174 lines (129 loc) · 3.56 KB
/
Copy pathmap.cpp
File metadata and controls
174 lines (129 loc) · 3.56 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include<iostream>
#include<map>
#include<iterator>
using namespace std;
int main()
{
//empty map container
map<int,string> ms;
//here key is an integer type and values are of string data type
map<int,int> mi;
ms[1]="heyy";
ms[16]="i";
ms[4]="really";
ms[7]="love";
ms[9]="programming";
ms.insert({6,"not really"});
cout<<"before erase : ";
for(auto i:ms)
{
cout<<i.first<<":"<<i.second<<" ";
}
cout<<endl;
cout<<"finding 4 : "<<ms.count(4)<<endl;
ms.erase(4);
cout<<"after erasing key 4 : ";
for(auto i:ms)
{
cout<<i.first<<":"<<i.second<<" ";
}
cout<<endl;
auto it=ms.find(6);
cout<<"\nprinting after a iterator pointing at key 6 : " <<endl;
for(auto i=it;i!=ms.end();i++)
{
cout<<(*i).first<<":"<<(*i).second<<" ";
}
cout<<endl<<endl<<endl;
// insert elements in random order
mi.insert(pair<int, int>(1, 40));
mi.insert(pair<int, int>(2, 30));
mi.insert(pair<int, int>(3, 60));
mi.insert(pair<int, int>(4, 20));
mi.insert(pair<int, int>(5, 50));
mi.insert(pair<int, int>(6, 50));
mi.insert(pair<int, int>(7, 10));
// printing map mi
map<int, int>::iterator itr;
cout << "\nThe map mi is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = mi.begin(); itr != mi.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
// assigning the elements from mi to copymi
map<int, int> copymi(mi.begin(), mi.end());
// print all elements of the map copymi
cout << "\nThe map copymi after"<< " assign from mi is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = copymi.begin(); itr != copymi.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// remove all elements up to
// element with key=3 in copymi
cout << "\ncopymi after removal of"
" elements less than key=3 : \n";
cout << "\tKEY\tELEMENT\n";
copymi.erase(copymi.begin(), copymi.find(3));
for (itr = copymi.begin(); itr != copymi.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// remove all elements with key = 4
int num;
num = copymi.erase(4);
cout << "\ncopymi.erase(4) : ";
cout << num << " removed \n";
cout << "\tKEY\tELEMENT\n";
for (itr = copymi.begin(); itr != copymi.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// lower bound and upper bound for map gquiz1 key = 5
cout << "mi.lower_bound(5) : "
<< "\tKEY = ";
cout << mi.lower_bound(5)->first << '\t';
cout << "\tELEMENT = "
<< mi.lower_bound(5)->second << endl;
cout << "mi.upper_bound(5) : "
<< "\tKEY = ";
cout << mi.upper_bound(5)->first << '\t';
cout << "\tELEMENT = "
<< mi.upper_bound(5)->second << endl;
//find()
/*
// C++ program for illustration
// of map::find() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialize container
map<int, int> mp;
// Insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 20 });
mp.insert({ 4, 50 });
cout << "Elements from position of 3 in the map are : \n";
cout << "KEY\tELEMENT\n";
// find() function finds the position
// at which 3 is present
for (auto itr = mp.find(3); itr != mp.end(); itr++) {
cout << itr->first << '\t' << itr->second << '\n';
}
return 0;
}
Output
The elements from position 3 in map are :
KEY ELEMENT
3 20
4 50
*/
return 0;
}