一文搞懂c++自定义排序规则对结构体排序
一文搞懂c++自定义排序规则对结构体排序
·
1、使用函数指针
#include <iostream>
#include <vector>
#include <algorithm>
struct Person {
std::string name;
int age;
};
//自定义升序排序函数
bool Compare(const Person& a, const Person& b)
{
return a.age < b.age;
}
int main() {
// 创建一个包含多个Person对象的向量
std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
//排序
std::sort(people.begin(), people.end(), Compare);
// 输出排序后的结果
for (const auto& person : people) {
std::cout << person.name << ": " << person.age << std::endl;
}
return 0;
}
2、使用lambda表达式
#include <iostream>
#include <vector>
#include <algorithm>
struct Person {
std::string name;
int age;
};
int main() {
// 创建一个包含多个Person对象的向量
std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
//排序
std::sort(people.begin(), people.end(), [](const Person& a, const Person& b){return a.age < b.age;});
// 输出排序后的结果
for (const auto& person : people) {
std::cout << person.name << ": " << person.age << std::endl;
}
return 0;
}
3、使用仿函数(函数对象)
#include <iostream>
#include <vector>
#include <algorithm>
struct Person {
std::string name;
int age;
};
//仿函数(函数对象)
struct Comparest{
bool operator()(const Person& a, const Person& b) const {
return a.age < b.age;
}
};
int main() {
// 创建一个包含多个Person对象的向量
std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
//排序
std::sort(people.begin(), people.end(), Comparest());
// 输出排序后的结果
for (const auto& person : people) {
std::cout << person.name << ": " << person.age << std::endl;
}
return 0;
}
4、重载运算符
#include <iostream>
#include <vector>
#include <algorithm>
struct Person {
std::string name;
int age;
//自定义升序排序算法(结构体内部重载)
bool operator<(const Person& a) const
{
return age < a.age; //如果是age > a.age,则是降序排列
}
};
////自定义升序排序算法(结构体外部重载)
//bool operator<(const Person& a, const Person& b)
//{
// return a.age < b.age; //如果是a.age > b.age,则是降序排列
//}
int main() {
// 创建一个包含多个Person对象的向量
std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
//排序,重载了运算符,不需要指定排序算法了
std::sort(people.begin(), people.end());
// 输出排序后的结果
for (const auto& person : people) {
std::cout << person.name << ": " << person.age << std::endl;
}
return 0;
}
map用结构体作为key的方法1,利用仿函数
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
struct Person {
std::string name;
int age;
};
//仿函数(函数对象)
struct Comparest{
bool operator()(const Person& a, const Person& b) const {
return a.age < b.age;
}
};
int main() {
// 创建一个包含多个Person对象的map
std::map<Person, int, Comparest> peoplemap;
peoplemap[{"Alice", 30}] = 0;
peoplemap[{"Bob", 25}] = 1;
peoplemap[{"Charlie", 35}] = 2;
for(const auto& per:peoplemap){
cout<<per.first.name<<" "<<per.first.age<<endl;
}
return 0;
}
map用结构体作为key的方法2,重载操作符
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
struct Person {
std::string name;
int age;
bool operator<(const Person& a) const{
return age < a.age;
}
};
int main() {
// 创建一个包含多个Person对象的map
std::map<Person, int> peoplemap;
peoplemap[{"Alice", 30}] = 0;
peoplemap[{"Bob", 25}] = 1;
peoplemap[{"Charlie", 35}] = 2;
for(const auto& per:peoplemap){
cout<<per.first.name<<" "<<per.first.age<<endl;
}
return 0;
}
更多推荐
所有评论(0)