结构体排序

本文提供了一个使用C++进行结构体数组排序的例子。通过定义一个结构体包含整型和浮点型变量,并实现一个比较函数来按浮点型字段降序排序。最后展示了排序后的结果。

这是一个炒鸡水题,大佬们不要喷,但我确实容易忘。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct st{
    int n;
    float t;
};
struct st c[10];
bool duke(st a,st b)
{
    return a.t > b.t;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int i = 0;i < t;i ++)
    {
        scanf("%d%g",&c[i].n,&c[i].t);
    }
    sort(c,c+t,duke);
    for(int i = 0;i < t;i++)
    {
        printf("%d %g\n",c[i].n,c[i].t);
    }
    return 0;
}

虽然很简单,但是总是忘,所以在这里发布一下。#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct st{
    int n;
    float t;
};
struct st c[10];
bool duke(st a,st b)
{
    return a.t > b.t;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int i = 0;i < t;i ++)
    {
        scanf("%d%g",&c[i].n,&c[i].t);
    }
    sort(c,c+t,duke);
    for(int i = 0;i < t;i++)
    {
        printf("%d %g\n",c[i].n,c[i].t);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/DukeLv/p/7995221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值