关于sort函数的入口函数cmp函数的一点心得

 

要求:有一个关于学生信息的结构体数组,有班级,成绩,年龄等信息,现要求一学生班级为主关键字排序,如果班级相同,则按照成绩排序,如果成绩相同,则按照年龄排序。

 

参考代码

#include <iostream>

#include <algorithm>

#include <vector>

 

using namespace std;

struct student

{

         int Class;

         int rank;

         int age;   

};

vector<student> stu;

 

bool cmp(const student &a,const student &b)

{

         if(a.Class!=b.Class)

         {

                   return a.Class<b.Class;  

         }       

         else if(a.rank!=b.rank)

         {

                   return a.rank<b.rank;     

         }

         else

         {

                   return a.age<b.age;        

         }

}

int main()

{

         student tmp;

         int num = 5;

         while(num--)

         {

                   cin>>tmp.Class>>tmp.rank>>tmp.age;

                   stu.push_back(tmp);       

         }

         sort(stu.begin(),stu.end(),cmp);

         for(int i=0;i<5;i++)

         {

                   cout<<stu[i].Class<<"\t"<<stu[i].rank<<"\t"<<stu[i].age<<endl;  

         }

         system("pause");

         return 0; 

}

 

参考数据:

 

1 4 1

1 3 3

2 3 3

2 3 1

4 2 3

 

运行结果:

 

 

 

要求:

有一字符串,由字母和数字组成,字母有大写和小写之分,现要使数字夹在小写字母和大写字母之间,而且字母遵循字典序,数字从小到大排序。

        

参考数据:

ofahIUGH5092foiIUIOq2t02ogihaohgawi5209GHAOIUHG

参考代码:

#include <iostream>

#include <algorithm>

 

#define SIZE 100

#define Is_Num(x) (x<='9'&&x>='0')

#define Is_LowCase(x) (x>='a'&&x<='z')

#define Is_UpCase(x) (x>='A'&&x<'Z')

 

using namespace std;

 

bool cmp(const char &a,const char &b)

{

         if(Is_Num(a)&&Is_Num(b))

         {

                   return a<b;      

         }       

         else if(Is_LowCase(a)&&Is_LowCase(b))

         {

                   return a<b;      

         }

         else if(Is_UpCase(a)&&Is_UpCase(b))

         {

                   return a<b;      

         }

         else if(Is_Num(a)&&Is_LowCase(b))

         {

                   return true;     

         }

         else if(Is_Num(b)&&Is_LowCase(a))

         {

                   return false;    

         }

         else if(Is_Num(a)&&Is_UpCase(b))

         {

                   return false;    

         }

         else if(Is_Num(b)&&Is_UpCase(a))

         {

                   return true;     

         }

         else if(Is_LowCase(a)&&Is_UpCase(b))

         {

                   return false;    

         }

         else if(Is_LowCase(b)&&Is_UpCase(a))

         {

                   return true;

         }

}

 

int main()

{

         char a[SIZE];

         while(cin>>a)

         {

                   int len = strlen(a);

                   sort(a,a+len,cmp);

                   cout<<"after sort:"<<endl;

                   cout<<a<<endl;       

         }

         return 0; 

}

 

运行结果:

 

 

要求:给一堆数字,有正有负,要求把所有的负数拿到正数前面来,且正数与正数的相对位置,负数与负数的相对位置都不能变。

 

参考数据:

15

 

3 4 -3 4 -5 -8 5 1 6 -2 5 -1 5 -3 -4

 

参考代码:

#include <iostream>

#include <algorithm>

#define SIZE 100

 

using namespace std;

 

bool cmp(const int &a,const int &b)

{

         if(a<0&&b>0)

         {

                   return true;     

         }

         return false;

}

 

int main()

{

         int array[SIZE];

         int length;

         while(cin>>length)

         {

                   for(int i=0;i<length;i++)

                   {

                            cin>>array[i];  

                   }       

                   sort(array,array+length,cmp);

                   cout<<"after sort:"<<endl;

                   for(int i=0;i<length;i++)

                   {

                            cout<<array[i]<<" ";        

                   }

         }

         return 0;

}

运行结果:

 

最后 cmp函数可以用greater<int>();(按总大到小的顺序排序)less<int>();(从小到大排序)。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值