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
| #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <map> using namespace std;
class Job { public: int money; int hard; public: Job(int money, int hard) { this->money = money; this->hard = hard; } }; class JobComparator { public: bool operator()(const Job &o1, const Job &o2) const { return o1.hard != o2.hard ? (o1.hard < o2.hard) : (o1.money > o2.money); } }; void test1() { Job o1(1, 1), o2(1, 2), o3(2, 1), o4(2, 2), o5(7, 2); vector<Job> jobs = {o1, o2, o3, o4, o5}; cout << "sorted by JobComparator()" << endl; stable_sort(jobs.begin(), jobs.end(), JobComparator()); for (auto &it : jobs) { cout << it.hard << " " << it.money << endl; }
cout << "sorted by lambda" << endl; auto cmp = [](const Job &o1, const Job &o2) -> bool { return o1.hard != o2.hard ? (o1.hard < o2.hard) : (o1.money > o2.money); }; stable_sort(jobs.begin(), jobs.end(), cmp); for (auto &it : jobs) { cout << it.hard << " " << it.money << endl; }
cout << "priority_queue<Job, vector<Job>, JobComparator>" << endl; priority_queue<Job, vector<Job>, JobComparator> pq(jobs.begin(), jobs.end()); while (!pq.empty()) { Job cur = pq.top(); pq.pop(); cout << cur.hard << " " << cur.money << endl; } }
void test2() { int x = 1; int y = 2; auto plus = [=](int a, int b) mutable -> int { x++; return x + y + a + b; }; int c = plus(1, 2); cout << c << " " << x << endl; }
int main() { test1(); return 0; }
|