-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path14_07.cpp
86 lines (67 loc) · 1.65 KB
/
14_07.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
#include <bits/stdc++.h>
using namespace std;
class Employee {
public:
int id;
int salary;
string name;
Employee(int id, int salary, string name) :
id(id), salary(salary), name(name) {
}
bool operator <(const Employee & c2) {
return std::tie(id, salary, name) <
std::tie(c2.id, c2.salary, c2.name);
}
void print() {
cout << id << " " << name << " " << salary << "\n";
}
};
void test1() {
vector<Employee> emps;
emps.push_back( { 9, 500, "ali" });
emps.push_back( { 1, 1000, "mostafa" });
emps.push_back( { 5, 700, "hani" });
sort(emps.begin(), emps.end()); // overloaded <
for (auto &emp : emps)
emp.print();
}
class EmployeeComparatorId {
public:
bool operator ()(const Employee & c1, const Employee & c2) {
return c1.id < c2.id;
}
};
class EmployeeComparatorSalary {
public:
bool operator ()(const Employee & c1,
const Employee & c2) {
return c1.salary < c2.salary;
}
};
void test2() {
vector<Employee> emps;
emps.push_back( { 9, 500, "ali" });
emps.push_back( { 1, 1000, "mostafa" });
emps.push_back( { 5, 700, "hani" });
EmployeeComparatorSalary comparator =
EmployeeComparatorSalary();
sort(emps.begin(), emps.end(), comparator);
for (auto &emp : emps)
emp.print();
}
void test3() {
vector<Employee> emps;
emps.push_back( { 9, 500, "ali" });
emps.push_back( { 1, 1000, "mostafa" });
emps.push_back( { 5, 700, "hani" });
sort(emps.begin(), emps.end(), [](const Employee & c1,
const Employee & c2) {
return c1.salary < c2.salary;
});
for (auto &emp : emps)
emp.print();
}
int main() {
test3();
return 0;
}