介绍
利用sort对数组进行排序需要用到下面这个头文件
#include <algorithm>
sort默认对数组进行升序排序,通过自定义函数可以实现多种功能排序,也可以对字符串进行排序。
最简单的排序
对a[] = {5, 4, 3, 2, 1}进行升序排序
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[] = {5, 4, 3, 2, 1};
int main()
{
sort(a, a + 5);
for (int i = 0; i <= 4; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
请注意如果想对a[0]到a[4]进行排序,最长的数组下标为4,需要写sort(a, a + 5);
也就是排到N就写sort(a, a + N + 1);
利用sort进行降序排序
有两种方法实现,先来介绍第一种。
使用自带greater进行排序
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[] = {1, 2, 4, 5, 3};
int main()
{
sort(a, a + 5, greater<int>());
for (int i = 0; i <= 4; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
greater<int>()
这里的()
不能省,<int>
貌似是对应数组的类型?初学先背下来再说吧
貌似还有个less
升序排序,个人感觉这个不是写不写都一样么… ,想了解的话自己查查吧。
手写cmp函数
bool cmp(int x, int y)
{
return x > y;
}
这里的怎么排序可以这样记 cmp函数里的参数x,y对应下面的x > y
,就是从大到小,如果改成x < y
就是从小到大。注意不能写>=
<=
不能出现等于号,原因不详 。
//演示代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[] = {1, 2, 4, 5, 3};
bool cmp(int x, int y)
{
return x > y;
}
int main()
{
sort(a, a + 5, cmp);
for (int i = 0; i <= 4; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
cmp函数进阶
如果想按照%7的余数进行降序排序呢?
bool cmp(int x, int y)
{
if(x % 7 != y % 7)
{
return x % 7 > y % 7;
}
else
{
return x > y;
}
}
解释一下:因为cmp函数里不能出现等号,所以如果两个数x,y的余数一样怎么返回值呢?答案是就让它按照x<y来排呗反正它俩也在一起,x>y应该也可以,这里自己多试几次就会理解了。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[] = {14, 24, 44, 54, 16};//样例可以自己多改几次,加深理解
bool cmp(int x, int y)
{
if(x % 7 != y % 7)
{
return x % 7 > y % 7;
}
else
{
return x > y;
}
}
int main()
{
sort(a, a + 5, cmp);
for (int i = 0; i <= 4; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
对结构体进行排序
说明:输入5个人的姓名和成绩,按成绩的大小进行降序排序
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct stu
{
int score;
char name[20];
};
bool cmp(stu x, stu y)
{
if (x.score != y.score)
{
return x.score > y.score;
}
else
{
return x.score > y.score;
}
}
int main()
{
stu a[10];
for (int i = 1; i <= 5; i++)
{
cin >> a[i].name >> a[i].score;
}
sort (a + 1, a + 5 + 1, cmp);
for (int i = 1; i <= 5; i++)
{
cout << a[i].name << ":" << a[i].score << endl;
}
return 0;
}
测试数据
zhangsan 80
lisi 89
wangwu 85
zhaoliu 89
tianqi 100
输出结果
tianqi:100
zhaoliu:89
lisi:89
wangwu:85
zhangsan:80
对字符进行排序
错误演示
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
char c[] = "bacde";
int main()
{
sort(c, c + strlen(c));
for (int i = 0; i < strlen(c); i++)
{
cout << c[i] << " ";
}
return 0;
}
输出什么都没有(也可能都是空格)
正确演示
降序
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
char c[] = "bacde";
int main()
{
sort(c, c + strlen(c), greater<char>());
for (int i = 0; i < strlen(c); i++)
{
cout << c[i] << " ";
}
return 0;
}
输出结果
e d c b a
升序
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
char c[] = "bacde";
int main()
{
sort(c, c + strlen(c), less<char>());
for (int i = 0; i < strlen(c); i++)
{
cout << c[i] << " ";
}
return 0;
}
输出结果
a b c d e
对字符串进行排序
后面再补充吧