sort 自定义排序使用方法

sort()排序函数是c++头文件include <algorithm>中的函数,它采用的是一种类似于快排的排序方式,时间复杂度  n*log2(n)。可以对 浮点数,整数,字符,字符串,结构体进行排顺序,排序方法不限于从大到小和从小到大两种,它可以根据使用者的排序规则进行排序

比较简单的:

按从大到小 

bool compare(int a,int b)
{
return a>b; //如果是从小到大,将">"变成“<”即可;
}

int mian ()
{
int arr[10];
for(int i=0;i<10;i++)
cin >>arr[i];
sort(arr,arr+10,compare); //compare 不传参数;
for(int i=0;i<10;i++)
cin >>arr[i];
}

如果是结构体的话

bool compare(struct a,struct b)

{

return a.()>b.();   //不能只写return a>b;因为此处说明的是按何种方式进排序;

}

 

当然,还有更为复杂的排序方式,以下便是核心代码;

 

核心代码:
    bool compare(T   a,T  b)    // T 为数据类型,根据具体的要传入的数据来求确定
{       if(   )  //先满足什么条件的先排序
           return  a>b; //按什么规则排序,此处按从大到小排序,
           else  if() //在排又满足什么条件的;
           return ......;
            ...........
}

拿个字符和字符串的例子来说吧

字符排序

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

bool com (char a,char b)
{
    return a<b;
}

int main()
{
    int n,k=0;
    char s[55],t[55];
    char *a,*b;

    cin >>n;        //输入字符个数


    scanf ("%s",s);
    scanf ("%s",t);

    a=s;
    b=t;

    sort(a,a+n,com);
    sort(b,b+n,com);

    for(int i=0; i<n; i++)
        cout <<a[i];

    cout <<endl;

    for(int i=0; i<n; i++)
        cout <<b[i];
    cout <<endl;

    for(int i=0; i<n; i++)   //比较两个字符串
        if(t[i]!=s[i])
        {
            k=1;
        }

        if(k==0)
            cout <<"两字符相等"<<endl;
        else
            cout <<"两字符串不相等";

    return 0;
}

 

字符串排顺序(注意,现在是串作为主体)

#include<stdio.h>
#include<algorithm>
#include<string>
#include<iostream>

using namespace std ;
bool compare(string a,string c)
{
    return a>c;

}
int main()
{
    int t,n, i;
    string s[10005];
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(i = 0; i < n; i++)
        {
            cin>>s[i];
        }
        sort(s,s+n,compare);            //sort对字符串排序只能是c++中string类型
        for(i = 0; i < n; i++)
            cout<<s[i];
        printf("\n");
    }

    return 0;
}

送一例题~~!!

                                         

                                                         最终排名

                                            Time Limit: 1000 ms                     Memory Limit:  65536 KiB

                                                                      Submit Statistic Discuss

Problem Description

第四届山东理工大学ACM网络编程擂台赛比赛完后需要产生一个最终排名,排名按照题数多少来决定。但是有太多的队伍参与,手动计算排名已经不能满足比赛的需求。现在有一份名单记录各个队伍的ID和做出的题目数,需要你写一个程序,产生最终的排名。

为了简化题目,这里的排名规则为:做出题目数量多的队伍排在前面,如果题数相等,保持输入时的相对顺序不要改变。

Input

第一行包含一个正整数T( 1 ≤ T ≤ 15),表示有T组测试数据。每组数据第一行有一个正整数N(1 <  N ≤ 10000),表示队伍数量。接下来N 行包含两个整数,1 ≤ ID ≤ 10^7, 0 ≤ M ≤ 100。ID为队伍的编号,M为做出的题数。

 

Output

每组数据输出包含N行,第i行有两个整数,ID和M表示排在第i位的队伍的ID和做出的题数。

 

Sample Input

181 216 311 220 33 526 47 122 4

Sample Output

3 526 422 416 320 31 211 27 1

 

代码:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

struct stu
{   string id;
    int ac,num;
};

bool com(stu a,stu b)
{
    if(a.ac!=b.ac)     //先把做题数不同的进行排序
    return a.ac>b.ac;  //按做题多少进行降序排序
    else
    return a.num<b.num;

}
int main()
{
    stu s[1050];
    int t,n;

    cin >>t;

    while (t--)
    {
        cin >>n;
        for(int i=0;i<n;i++)
        {
            cin >>s[i].id;
            cin >>s[i].ac;
            s[i].num=i;
        }
        sort (s,s+n,com);

        for(int i=0;i<n;i++)
            cout << s[i].id<<" "<<s[i].ac << endl;
    }


    return 0;
}

 

  • 8
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值