目录&索引
1 sort 函数中的 cmp
1.1 重载小于运算符
sort 重载小于运算符,重载示例如下:
#include <iostream>
#include <algorithm>
using namespace std;
struct Student {
string name;
int num;
int age;
bool operator<(const Student &stu) const {
return stu.age < this->age;
}
};
int main() {
Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
{"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
sort(stu, stu + 5);
for (int i = 0; i < 5; ++i) {
cout << stu[i].name << " is " << stu[i].age << endl;
}
return 0;
}
1.2 自定义 cmp 函数
sort 自定义 cmp 函数,示例如下:
#include <iostream>
#include <algorithm>
using namespace std;
struct Student {
string name;
int num;
int age;
};
bool cmp(const Student &stu1, const Student &stu2) {
return stu2.age < stu1.age;
}
int main() {
Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
{"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
sort(stu, stu + 5, cmp);
for (int i = 0; i < 5; ++i) {
cout << stu[i].name << " is " << stu[i].age << endl;
}
return 0;
}
1.3 自定义仿函数
sort 自定义仿函数,示例如下:
#include <iostream>
#include <algorithm>
using namespace std;
struct Student {
string name;
int num;
int age;
};
struct cmp {
bool operator()(const Student &stu1, const Student &stu2) {
return stu2.age < stu1.age;
}
};
int main() {
Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
{"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
sort(stu, stu + 5, cmp()); // cmp() 为匿名对象
for (int i = 0; i < 5; ++i) {
cout << stu[i].name << " is " << stu[i].age << endl;
}
return 0;
}
1.4 自定义 lambda 表达式
sort 自定义 lambda 表达式,示例如下:
#include <iostream>
#include <algorithm>
using namespace std;
struct Student {
string name;
int num;
int age;
};
int main() {
Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
{"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
sort(stu, stu + 5,
[](const Student &stu1, const Student &stu2) { return stu2.age < stu1.age; });
for (int i = 0; i < 5; ++i) {
cout << stu[i].name << " is " << stu[i].age << endl;
}
return 0;
}
1.5 输出结果(逆序)
年龄从大到小输出,结果如下:
Ma Zi is 21
Wang Er is 20
Li Si is 19
Zhang san is 18
Lucy is 17
2 priority_queue 中的 cmp
2.1 重载小于运算符
priority_queue 重载小于运算符,重载示例如下:
#include <iostream>
#include <queue>
using namespace std;
struct Student {
string name;
int num;
int age;
bool operator<(const Student &stu) const {
return stu.age < this->age;
}
};
int main() {
Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
{"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
priority_queue<Student> q;
for (int i = 0; i < 5; ++i) {
q.push(stu[i]);
}
while (!q.empty()) {
cout << q.top().name << " is " << q.top().age << endl;
q.pop();
}
return 0;
}
2.2 自定义仿函数
priority_queue 自定义仿函数,示例如下:
#include <iostream>
#include <queue>
using namespace std;
struct Student {
string name;
int num;
int age;
};
struct cmp {
bool operator()(const Student &stu1, const Student &stu2) {
return stu2.age < stu1.age;
}
};
int main() {
Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
{"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
priority_queue<Student, vector<Student>, cmp> q;
for (int i = 0; i < 5; ++i) {
q.push(stu[i]);
}
while (!q.empty()) {
cout << q.top().name << " is " << q.top().age << endl;
q.pop();
}
return 0;
}
2.3 自定义 lambda 表达式
priority_queue 自定义 lambda 表达式,示例如下:
#include <iostream>
#include <queue>
using namespace std;
struct Student {
string name;
int num;
int age;
};
auto cmp = [](const Student &stu1, const Student &stu2) {
return stu2.age < stu1.age;
};
int main() {
Student stu[5] = {{"Zhang san", 1, 18}, {"Li Si", 2, 19}, {"Wang Er", 3, 20},
{"Ma Zi", 4, 21}, {"Lucy", 5, 17}};
priority_queue<Student, vector<Student>, decltype(cmp)> q(cmp);
for (int i = 0; i < 5; ++i) {
q.push(stu[i]);
}
while (!q.empty()) {
cout << q.top().name << " is " << q.top().age << endl;
q.pop();
}
return 0;
}
2.4 输出结果(小顶堆)
大顶堆改为小顶堆,年龄从小到大输出,结果如下:
Lucy is 17
Zhang san is 18
Li Si is 19
Wang Er is 20
Ma Zi is 21
3 结论
sort 的 cmp 函数,priority_queue 的 cmp 类型。