对于一个连续存储空间的数组进行排序,我们可以使用C++中提供 的sort。
C++中已经为我们写好了sort函数,需要调用是我们要引入一个头文件
#inlcide<algorithm>
要提醒的是sort可以排序任何类型的元素,包括自行定义的结构体。
sort用例
int main()
{
int arr[] = {0,-3,7,9,1,4};
//对arr[0]到arr[5]进行排序,按照从大到小的顺序排列
sort(arr+0,arr+6,greater<int>());
for(int i=0;i<6;i++)
cout<<arr[i]<<" ";
return 0;
}
使用cmp函数
cmp为bool函数,可以自定义sort排序的规则。
如果不重写cmp函数,sort函数默认为从小到大排序。
从小到大排序
//升序排序
bool cmp(int x,int y)
{
return x < y;
}
从大到小排序
//降序排序
bool cmp(int x,int y)
{
return x > y;
}
有N个学生的数据,要进行升序排序。将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相同则按照学生的年龄排序,并输出N个学生排序后的信息。
typedef struct
{
int score;
string name;
int age;
}student;
bool cmp(student a, student b)
{
if(a.score != b.score)
return a.score < b.score;
else if(a.name != b.name)
return a.name < b.name;
else
return a.age < b.age;
}
例题
来源:http://acm.nefu.edu.cn/problemShow.php?problem_id=2068
//这里只重写cmp比较函数
typedef struct
{
string name;
int year;
int month;
int day;
int position;
}stu;
bool cmp(stu a,stu b)
{
if(a.year != b.year)
return a.year < b.year;
else if(a.month != b.month)
return a.month < b.month;
else if(a.day != b.day)
return a.day < b.day;
else
return a.position > b.position;
}
数组的sort排序
test1
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int a[31];
int main()
{
int N, k;
cin>>N;
for(int i=0;i<N;i++)
cin>>a[i];
cin>>k;
sort(a+0,a+N,greater<int>());//降序排序
double ave = 0;
for(int i=0;i<k;i++)
ave += a[i];
ave /= k;
printf("%.2f",ave);
return 0;
}
test2
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;
int a[31];
int b[7];
int main()
{
memset(b,0,sizeof(b));
int N;
cin>>N;
for(int i=0;i<N;i++)
cin>>a[i];
sort(a+0,a+N,greater<int>());//降序排序
for(int i=0;i<N;i++){
if(a[i] == 100)
b[1]++;
else if(a[i]>=90 && a[i] <= 99)
b[2]++;
else if(a[i]>=80 && a[i] <= 89)
b[3]++;
else if(a[i]>=70 && a[i] <= 79)
b[4]++;
else if(a[i]>=60 && a[i] <= 69)
b[5]++;
else
b[6]++;
}
//输出
for(int i=0;i<N;i++)
cout<<a[i]<<endl;
for(int i=1;i<6;i++)
cout<<b[i]<<" ";
cout<<b[6];
return 0;
}
test3
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;
int a[105];
bool cmp(int x,int y)
{
if(x%3 != y%3)//按照余数从小到大排序
return x%3 < y%3;
else//余数相等,按照正整数从小到大排序
return x<y;
}
int main()
{
int N;
cin>>N;
for(int i=0;i<N;i++)
cin>>a[i];
sort(a+0,a+N,cmp);
for(int i=0;i<N;i++)
cout<<a[i]<<" ";
return 0;
}
结构体的sort排序
一个程序中可以定义多种排序方法,自行定义函数名并调用即可。
test
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct Student {
string name;
int score[4];
};
//按照姓名字典序升序排列
bool cmp_name(Student x, Student y) {
return x.name < y.name;
}
//先按照第一门课的成绩降序排列,
//如果第一门课成绩相同,按照第二门课成绩降序排列
//如果第二门课成绩相同,按照第三门课成绩降序排列,依此类推。
bool cmp_score(Student x, Student y) {
if (x.score[0] != y.score[0]) {
return x.score[0] > y.score[0];
}
if (x.score[1] != y.score[1]) {
return x.score[1] > y.score[1];
}
if (x.score[2] != y.score[2]) {
return x.score[2] > y.score[2];
}
return x.score[3] > y.score[3];
}
int main() {
Student stu[3];
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_name);
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;
}
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;
}
由于sort排序这个知识点比较简单,不再赘述。
笔者认为sort函数的重点在于如何cmp函数