-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomMapKey.cpp
53 lines (42 loc) · 976 Bytes
/
CustomMapKey.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
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(): name(""), age(0) {
}
Person(const Person &other) {
cout << "Copy constructor running!" << endl;
name = other.name;
age = other.age;
}
Person(string name, int age): name(name), age(age) {
}
void print() const {
cout << name << ": " << age << flush;
}
bool operator<(const Person &other) const {
if(name == other.name) {
return age < other.age;
} else {
return name < other.name;
}
}
};
int _main7(int argc, _TCHAR* argv[]) {
map<Person, int> people;
people[Person("Mike", 40)] = 40;
people[Person("Sue", 30)] = 30;
people[Person("Raj", 20)] = 20;
for(map<Person, int>::iterator it = people.begin(); it != people.end(); it++) {
cout << it->second << ": " << flush;
it->first.print();
cout << endl;
}
return 0;
}