forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap2.cpp
More file actions
67 lines (56 loc) · 2.12 KB
/
Copy pathmap2.cpp
File metadata and controls
67 lines (56 loc) · 2.12 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
#include <iostream>
#include<string>
#include<map>
using namespace std;
class StudentID {
private:
int id;
int year;
public:
explicit StudentID(int i, int y=2022) : id(i), year(y) { }
bool operator<(const StudentID& b) const {
if (this->id == b.id) {
return this->year < b.year;
}
else {
return this->id < b.id;
}
}
friend ostream& operator<<(ostream& strm, const StudentID& m) {
strm << " ID : " << m.id << " " << m.year;
return strm;
}
};
int main() {
map<StudentID, string> studentIDs;
studentIDs.insert(pair<StudentID, string>(StudentID(1001134,2019), "Doyle Myers"));
studentIDs.insert(pair<StudentID, string>(StudentID(1075389,2019), "Maureen Reyes"));
studentIDs.insert(pair<StudentID, string>(StudentID(1162458,2022), "Terence Peters"));
studentIDs.insert(pair<StudentID, string>(StudentID(1779365,2020), "Archie Bryant"));
studentIDs.insert(pair<StudentID, string>(StudentID(2493890,2021), "Margie Bell"));
studentIDs.insert(pair<StudentID, string>(StudentID(2788773,2022), "Robin Newman"));
studentIDs.insert(pair<StudentID, string>(StudentID(3009755,2021), "Kristy Sharp"));
// Print the map
cout << "Student IDs:" << endl;
for (auto const &pair : studentIDs) {
cout << " " << pair.first << " " << pair.second << endl;
}
cout << endl << "find() testing:" << endl;
StudentID idToSearch(2788773);
if (studentIDs.find(idToSearch) == studentIDs.end()) {
cout << " Unable to find record for student ID " << idToSearch << endl;
}
else {
cout << " StudentID: " << idToSearch << " belongs to "
<< studentIDs.at(idToSearch) << endl;
}
StudentID idToSearch2(1111111);
if (studentIDs.find(idToSearch2) == studentIDs.end()) {
cout << " Unable to find record for student ID " << idToSearch2 << endl;
}
else {
cout << " StudentID: " << idToSearch2 << " belongs to "
<< studentIDs.at(idToSearch2) << endl;
}
return 0;
}