C++sort()对结构体类型排序

本文详细介绍如何使用C++对自定义结构体进行排序,包括使用比较函数和重载小于运算符两种方法,以及在结构体内和结构体外实现的不同方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struct类型排序

#include <iostream>
#include <algorithm>
#include<vector>
using namespace std;
struct Date
{
    int a;
    int b;
};
bool com(const Date x,const Date y)
{
    if(x.a==y.a)
        return x.b>y.b;
    return x.a>y.a;
}
int main()
{
    Date dat[5] = {{1,10},{2,9},{3,8},{3,7},{5,6}};
    sort(dat,dat+5,com); //按照第1个数由大到小排序,当第1个数相同时,按照第2个数由大到小排序
    for(int i=0;i<5;i++)
    {
        cout<<dat[i].a<<" "<<dat[i].b<<endl;
    }
    return 0;
}

输出:
5 6
3 8
3 7
2 9
1 10

bool operator型写法

operator重载小于运算符,可以设定由小到大排序,也可以设定由大到小排序
两种写法,可以写在struct内,也可以写在struct外。
这种写法区分于那种重载()运算符写法。

在struct外重载小于运算符

#include <iostream>
#include <algorithm>
#include<vector>
using namespace std;
struct Date
{
    int a;
    int b;
    
};
bool operator<(const Date &x,const Date &y)
{
    if(x.a == y.a)
        return x.b>y.b;
    return x.a>y.a;
}
int main()
{
    Date dat[5] = {{1,10},{2,9},{3,8},{3,7},{5,6}};
    sort(dat,dat+5);
    for(int i=0;i<5;i++)
    {
        cout<<dat[i].a<<" "<<dat[i].b<<endl;
    }
    return 0;
}
输出:
5 6
3 8
3 7
2 9
1 10

在struct内重载小于运算符

#include <iostream>
#include <algorithm>
#include<vector>
using namespace std;
struct Date
{
    int a;
    int b;
    bool operator < (const Date &y) const
    {
        return b>y.b; //设定按照b由大到小排列
    }
};
int main()
{
    Date dat[5] = {{1,10},{2,9},{3,8},{3,7},{5,6}};
    sort(dat,dat+5);
    for(int i=0;i<5;i++)
    {
        cout<<dat[i].a<<" "<<dat[i].b<<endl;
    }
    return 0;
}
输出:
1 10
2 9
3 8
3 7
5 6
### C++ 中对结构体进行排序的方法 #### 使用 `std::sort` 对结构体排序 为了实现对自定义结构体类型排序,可以采用两种主要方法: - **重载 `<` 运算符** 当希望基于某个特定字段(如年龄)来进行默认排序时,在结构体内重载小于号运算符是一种常见做法。这使得可以直接利用标准库中的 `std::sort()` 函数而无需额外提供比较器。 ```cpp #include <iostream> #include <algorithm> #include <vector> struct Person { std::string name; int age; bool operator<(const Person& other) const { // 将此函数声明为const成员函数[^3] return age < other.age; } }; int main() { std::vector<Person> people = {{"Alice", 25}, {"Bob", 20}, {"Charlie", 30}}; std::sort(people.begin(), people.end()); for (auto&& person : people) { std::cout << "Name: " << person.name << ", Age: " << person.age << '\n'; } return 0; } ``` - **传递自定义比较函数** 如果想要根据不同的条件灵活调整排序逻辑,则可以通过向 `std::sort()` 提供第三个参数作为定制化的比较谓词来达成目的。这种方式允许更复杂的排序规则而不必改变原始数据结构的设计。 ```cpp bool compare_by_name(const Person &a, const Person &b) { return a.name < b.name; } // 或者使用lambda表达式简化写法 auto lambda_compare_by_name = [](const Person &a, const Person &b){ return a.name < b.name; }; int main(){ std::vector<Person> people = { /* 初始化列表 */ }; // 使用命名函数或Lambda表达式作为第三参数 std::sort(people.begin(), people.end(), compare_by_name); // Lambda版本 // std::sort(people.begin(), people.end(), lambda_compare_by_name); // 输出结果... } ``` 对于更加复杂的数据类型比如题目提到的学生信息表单(包含姓名、性别等多个属性),同样适用上述原则;只需确保所选的关键字能够唯一区分各个记录即可[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值