-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cpp
199 lines (151 loc) · 5.65 KB
/
map.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "main.hpp"
int mapAccessOperator(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] Operator[] --- *" << std::endl;
STDFT::map<char,std::string> myMap;
myMap['a']="1";
myMap['b']="2";
myMap['c']="3";
myMap['d']="4";
myMap['e']=myMap['d'];
outfile << "myMap['a'] is " << myMap['a'] << '\n';
outfile << "myMap['b'] is " << myMap['b'] << '\n';
outfile << "myMap['c'] is " << myMap['c'] << '\n';
outfile << "myMap['d'] is " << myMap['d'] << '\n';
outfile << "myMap['e'] is " << myMap['e'] << '\n';
outfile << "myMap now contains " << myMap.size() << " elements.\n";
return 0;
}
int mapAt(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] At --- *" << std::endl;
STDFT::map<char,std::string> myMap;
myMap['a']="1";
myMap['b']="2";
myMap['c']="3";
myMap['d']="4";
myMap['e']=myMap['d'];
outfile << "myMap['a'] is " << myMap.at('a') << '\n';
outfile << "myMap['b'] is " << myMap.at('b') << '\n';
outfile << "myMap['c'] is " << myMap.at('c') << '\n';
outfile << "myMap['d'] is " << myMap.at('d') << '\n';
outfile << "myMap['e'] is " << myMap.at('e') << '\n';
outfile << "myMap now contains " << myMap.size() << " elements.\n";
return 0;
}
int mapBegin(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] Begin End --- *" << std::endl;
STDFT::map<char,int> mymap;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
// show content:
for (STDFT::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
outfile << it->first << " => " << it->second << '\n';
return 0;
}
int mapClear(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] Clear --- *" << std::endl;
STDFT::map<char,int> myMap;
myMap['a']=10;
myMap['b']=20;
myMap['c']=30;
outfile << "myMap.size() is " << myMap.size() << '\n';
myMap.clear();
outfile << "myMap.size() is " << myMap.size() << '\n';
return 0;
}
int mapCount(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] Count --- *" << std::endl;
STDFT::map<char,int> myMap;
myMap['a']=10;
myMap['b']=20;
myMap['c']=30;
outfile << "myMap.count('a') is " << myMap.count('a') << '\n';
outfile << "myMap.count('b') is " << myMap.count('b') << '\n';
outfile << "myMap.count('c') is " << myMap.count('c') << '\n';
outfile << "myMap.count('d') is " << myMap.count('d') << '\n';
return 0;
}
int mapEmpty(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] Empty --- *" << std::endl;
STDFT::map<char,int> myMap;
outfile << "myMap.empty() is " << myMap.empty() << '\n';
myMap['a']=10;
myMap['b']=20;
myMap['c']=30;
outfile << "myMap.empty() is " << myMap.empty() << '\n';
return 0;
}
int mapErase(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] Erase --- *" << std::endl;
STDFT::map<char,int> myMap;
myMap['a']=10;
myMap['b']=20;
myMap['c']=30;
myMap['d']=40;
myMap['e']=50;
myMap['f']=60;
outfile << "myMap contains " << myMap.size() << " elements.\n";
outfile << "myMap.erase('b')\n";
myMap.erase('b');
outfile << "myMap contains " << myMap.size() << " elements.\n";
outfile << "myMap.erase('e')\n";
myMap.erase('e');
outfile << "myMap contains " << myMap.size() << " elements.\n";
return 0;
}
int mapEqualRange(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] EqualRange --- *" << std::endl;
STDFT::map<char,int> myMap;
myMap['a']=10;
myMap['b']=20;
myMap['c']=30;
STDFT::pair<STDFT::map<char,int>::iterator,STDFT::map<char,int>::iterator> ret;
ret = myMap.equal_range('b');
outfile << "lower bound points to: ";
outfile << ret.first->first << " => " << ret.first->second << '\n';
outfile << "upper bound points to: ";
outfile << ret.second->first << " => " << ret.second->second << '\n';
return 0;
}
int mapFind(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] Find --- *" << std::endl;
STDFT::map<char,int> myMap;
myMap['a']=10;
myMap['b']=20;
myMap['c']=30;
STDFT::map<char,int>::iterator it = myMap.find('b');
if (it != myMap.end())
myMap.erase (it);
outfile << "elements in myMap:" << '\n';
outfile << "a => " << myMap.find('a')->second << '\n';
outfile << "c => " << myMap.find('c')->second << '\n';
return 0;
}
int mapInsert(std::ofstream &outfile) {
outfile << std::endl << "* [MAP] Insert --- *" << std::endl;
STDFT::map<char,int> myMap;
// first insert function version (single parameter):
myMap.insert ( STDFT::pair<char,int>('a',100) );
myMap.insert ( STDFT::pair<char,int>('z',200) );
STDFT::pair<STDFT::map<char,int>::iterator,bool> ret;
ret = myMap.insert ( STDFT::pair<char,int>('z',500) );
if (ret.second==false) {
outfile << "element 'z' already existed";
outfile << " with a value of " << ret.first->second << '\n';
}
// second insert function version (with hint position):
STDFT::map<char,int>::iterator it = myMap.begin();
myMap.insert (it, STDFT::pair<char,int>('b',300)); // max efficiency inserting
myMap.insert (it, STDFT::pair<char,int>('c',400)); // no max efficiency inserting
// third insert function version (range insertion):
STDFT::map<char,int> anotherMap;
anotherMap.insert(myMap.begin(),myMap.find('c'));
// showing contents:
outfile << "myMap contains:\n";
for (it=myMap.begin(); it!=myMap.end(); ++it)
outfile << it->first << " => " << it->second << '\n';
outfile << "anotherMap contains:\n";
for (it=anotherMap.begin(); it!=anotherMap.end(); ++it)
outfile << it->first << " => " << it->second << '\n';
return 0;
}