forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
57 lines (47 loc) · 1.76 KB
/
Copy pathmap.cpp
File metadata and controls
57 lines (47 loc) · 1.76 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
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main() {
map<string, int> grades;
// Add pairs to the map
grades.insert(pair<string, int>("Bruce", 84));
grades.insert(pair<string, int>("Rachel", 98));
grades.insert(pair<string, int>("Liam", 99));
grades.insert(pair<string, int>("Jackson", 90));
grades.insert(pair<string, int>("Anna", 92));
// Print the pairs in the map
cout << "Grades:" << endl;
for (map<string,int>::iterator itr=grades.begin(); itr!=grades.end(); itr++) {
cout << " " << itr->first << " " << itr->second << endl;
}
// Delete Jackson's grade
int gradeCount = grades.erase("Jackson");
cout << endl << "Removed " << gradeCount << " grades" << endl;
// Print the remaining grades
cout << endl << "Remaining grades:" << endl;
for (map<string,int>::iterator itr=grades.begin(); itr!=grades.end(); itr++) {
cout << " " << itr->first << " " << itr->second << endl;
}
// Search the map
cout << endl << "Searching for grades:" << endl;
string searchName = "Rachel";
if (grades.find(searchName) == grades.end()) {
cout << " Unable to find grade for " << searchName << endl;
}
else {
cout << " " << searchName << "'s grade is " << grades.at(searchName) << endl;
}
searchName = "Jackson";
if (grades.find(searchName) == grades.end()) {
cout << " Unable to find grade for " << searchName << endl;
}
else {
cout << " " << searchName << "'s grade is " << grades.at(searchName) << endl;
}
// Update a map entry
cout << endl << "Update Bruce's grade: " << endl;
grades.at("Bruce") += 5;
cout << "Bruce's updated grade is " << grades.at("Bruce") << endl;
return 0;
}