sort排序

sort()的使用方法
头文件
 在C++中使用sort()函数需要使用#include<algorithm>头文件。algorithm意为"算法",是C++的标准模版库(STL)中最重要的头文件之一,提供了大量基于迭代器的非成员模版函数。该头文件的详细使用方法以及包含的函数请参考:C++API之algorithm。

sort()基本使用方法


sort()函数可以对给定区间所有元素进行排序。它有三个参数sort(begin, end, cmp),

其中begin为指向待sort()的数组的第一个元素的指针,

end为指向待sort()的数组的最后一个元素的下一个位置的指针,

cmp参数为排序准则,cmp参数可以不写,如果不写的话,默认从小到大进行排序。

如果我们想从大到小排序可以将cmp参数写为greater<int>()就是对int数组进行排序,当然我们也可以倒序使用for循环,就不用记这个了

当然<>中我们也可以写double、long、float等等。如果我们需要按照其他的排序准则,那么就需要我们自己定义一个bool类型的函数来传入。比如我们对一个整型数组进行从大到小排序:
 

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

int main()
{
	int num[10] = {6,5,9,1,2,8,7,3,4,0};
	sort(num,num+10,greater<int>());
	for(int i=0;i<10;i++)
    {
		cout<<num[i]<<" ";
	}//输出结果:9 8 7 6 5 4 3 2 1 0
	
	return 0;
	
} 

自定义排序准则


 上面我们说到sort()函数可以自定义排序准则,以便满足不同的排序情况。使用sort()我们不仅仅可以从大到小排或者从小到大排,还可以按照一定的准则进行排序。比如说我们按照每个数的个位进行从大到小排序,我们就可以根据自己的需求来写一个函数作为排序的准则传入到sort()中。
我们可以将这个函数定义为:
 

bool cmp(int x,int y)
{
	return x % 10 > y % 10;
}

然后我们将这个cmp函数作为参数传入sort()中即可实现了上述排序需求。

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

bool cmp(int x,int y)
{
	return x % 10 > y % 10;
}

int main()
{
	int num[10] = {65,59,96,13,21,80,72,33,44,99};
	sort(num,num+10,cmp);
	for(int i=0;i<10;i++)
    {
		cout<<num[i]<<" ";
	}//输出结果:59 99 96 65 44 13 33 72 21 80
	
	return 0;
	
} 

对结构体进行排序

 sort()也可以对结构体进行排序,比如我们定义一个结构体含有学生的姓名和成绩的结构体Student,然后我们按照每个学生的成绩从高到底进行排序。首先我们将结构体定义为:

struct Student
{
	string name;
	int score;
	Student() {}
	Student(string n,int s):name(n),score(s) {}
};

完整代码:

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

struct Student
{
	string name;
	int score;
	Student() {}
	Student(string n,int s):name(n),score(s) {}
};

bool cmp_score(Student x,Student y)
{
	return x.score > y.score;
}

int main()
{
	Student stu[3];
	string n;
	int s;
	for(int i=0;i<3;i++)
    {
		cin>>n>>s;
		stu[i] = Student(n,s);
	}
	
	sort(stu,stu+3,cmp_score);
	
	for(int i=0;i<3;i++)
    {
		cout<<stu[i].name<<" "<<stu[i].score<<endl;
	}
	
	return 0;
}

再比如每一个学生有四科成绩,我们需要根据学生的四科成绩的平均分高低进行排名,那么这个cmp函数我们就可以定义为:

bool cmp_score(Student x,Student y)
{
	double average_x,average_y;
	average_x = (x.score[0]+x.score[1]+x.score[2]+x.score[3])/4;
	average_y = (y.score[0]+y.score[1]+y.score[2]+y.score[3])/4;
	return average_x > average_y;
}

完整代码:

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

struct Student
{
	string name;
	double score[4];
};

bool cmp_score(Student x,Student y)
{
	double average_x,average_y;
	average_x = (x.score[0]+x.score[1]+x.score[2]+x.score[3])/4;
	average_y = (y.score[0]+y.score[1]+y.score[2]+y.score[3])/4;
	return average_x > average_y;
}

int main()
{
	Student stu[3];
	string n;
	int s;
	for(int i=0;i<3;i++)
    {
		cin>>stu[i].name;
		for(int j=0;j<4;j++)
        {
			cin>>stu[i].score[j];
		}
	}
	
	sort(stu,stu+3,cmp_score);
	
	for(int i=0;i<3;i++)
    {
		cout<<stu[i].name<<" ";
		for(int j=0;j<4;j++)
        {
			cout<<stu[i].score[j]<<" ";
		}
		cout<<endl;
	}
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值