-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaps.cpp
115 lines (89 loc) · 2.76 KB
/
Maps.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
#include <iostream>
#include <map>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(): name(""), age(0) {}
Person(string name, int age) : name(name), age(age) {}
Person(const Person &other) {
cout << "Copy constructor used" << endl;
name = other.name;
age = other.age;
}
void print() const {
cout << name << ": " << age;
}
bool operator<(const Person &other) const {
if(name == other.name) {
return age < other.age;
}
else {
return name < other.name;
}
}
};
int main() {
map<string, int> ages;
// pair<string, int> addToMap("Peter", 84);
ages["Daniel"] = 12;
ages["Mike"] = 39;
ages["Bob"] = 47;
// ages.insert(addToMap);
// ages.insert(pair<string, int>("Peter", 84));
ages.insert(make_pair("Peter", 84));
// if(ages.find("Mike") != ages.end())
for(map<string, int>::iterator it=ages.begin(); it != ages.end(); it++) {
pair<string, int> age = *it;
cout << age.first << ": " << age.second << endl;
}
for(map<string, int>::iterator it=ages.begin(); it != ages.end(); it++) {
cout << it->first << ": " << it->second << endl;
}
ages["Bob"] = 58;
cout << ages["Bob"] << endl;
// Custom Value
map<int, Person> people;
people[0] = Person("Mike", 39);
people[1] = Person("Bob", 23);
people[2] = Person("Daniel", 12);
people.insert(make_pair(33, Person("Joe", 43)));
for(map<int, Person>::iterator it = people.begin(); it != people.end(); it++) {
cout << it->first << " - ";
it->second.print();
}
// Custom Key
map<Person, int> people2;
people2[Person("Mike", 43)] = 42;
people2[Person("Mike", 404)] = 123;
people2[Person("Jeff", 88)] = 1;
people2[Person("Sue", 37)] = 33;
for(map<Person, int>::iterator it = people2.begin(); it != people2.end(); it++) {
cout << it->second << " - " << flush;
it->first.print();
cout << endl;
}
// Multimap
multimap<int, string> lookup;
lookup.insert(make_pair(30, "John"));
lookup.insert(make_pair(19, "Jake"));
lookup.insert(make_pair(15, "Vicky"));
lookup.insert(make_pair(8, "Don"));
lookup.insert(make_pair(15, "Dylan"));
for(multimap<int, string>::iterator it=lookup.begin(); it != lookup.end(); it++) {
cout << it->first << ": " << it->second << endl;
}
cout << endl;
for(multimap<int, string>::iterator it=lookup.find(15); it != lookup.end(); it++) {
cout << it->first << ": " << it->second << endl;
}
cout << endl;
pair<multimap<int, string>::iterator, multimap<int, string>::iterator> its = lookup.equal_range(30);
auto its2 = lookup.equal_range(30);
for(multimap<int, string>::iterator it = its.first; it != its.second; it++) {
cout << it->first << ": " << it->second << endl;
}
return 0;
}