注意:使用sort函数需包含algorithm这一头文件。
且默认是从小到大排序。(升序)
#include<iostream>
#include<algorithm>
#include<ctime>
using namespace std;
int main() {
srand((unsigned)time(NULL));
int a[10];
for (int i = 0; i < 10; i++) {
a[i] = rand() % 10 + 1;
cout << a[i] << " ";
}
cout << endl;
sort(a, a + 10);//注意这里是a+10不是a+9!!
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
这一代码段的功能是对生成的10个随机数进行升序排序,效果如下:
如果要根据其他方法自定义排序的话,我们可以自己编写一个比较函数来实现。
案例1:如何编写一个程序使其按降序排序?
#include<iostream>
#include<algorithm>
#include<ctime>
bool compare(int a, int b)
{
return a > b;
}
using namespace std;
int main() {
srand((unsigned)time(NULL));
int a[10];
for (int i = 0; i < 10; i++) {
a[i] = rand() % 10 + 1;
cout << a[i] << " ";
}
cout << endl;
sort(a, a + 10,compare);//注意这里是a+10不是a+9!!
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
程序效果如下:
案例2:编写一个程序,使其个位按降序排列,若个位相同则十位按降序排列。(随机数为两位数序列)
第一种实现方式:
#include<iostream>
#include<algorithm>
#include<ctime>
bool compare(int a, int b)
{
return (a%10 > b%10)||((a%10==b%10)&&(a/10>b/10));
}
using namespace std;
int main() {
srand((unsigned)time(NULL));
int a[10];
for (int i = 0; i < 10; i++) {
a[i] = rand() % 90 + 10;
cout << a[i] << " ";
}
cout << endl;
sort(a, a + 10,compare);//注意这里是a+10不是a+9!!
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
第二种实现方式:
#include<iostream>
#include<algorithm>
#include<ctime>
bool compare(int a, int b)
{
if ((a % 10 > b % 10) || ((a % 10 == b % 10) && (a / 10 > b / 10)))
return true;
else
return false;
}
using namespace std;
int main() {
srand((unsigned)time(NULL));
int a[10];
for (int i = 0; i < 10; i++) {
a[i] = rand() % 90 + 10;
cout << a[i] << " ";
}
cout << endl;
sort(a, a + 10, compare);//注意这里是a+10不是a+9!!
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
程序实现效果如下:
案例3:对结构体进行排序
给学生按分数排序,若分数一样,则其姓名按字典序排序。
第一种实现方式:
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstring>
using namespace std;
struct student {
int grades=0;//初始化成员变量
string NaMe;
}st[10];
bool compare(const student& a, const student& b)
{
return (a.grades > b.grades) || ((a.grades == b.grades) && (a.NaMe < b.NaMe));;
}
int main() {
srand((unsigned)time(NULL));
string xm[10] = { "支文栋","党乐章","毛乐家","公冶彭魄","胥建树","禄锐利","周明诚","郝阳云","朱德容","端木和正" };
for (int i = 0; i < 10; i++) {
st[i].NaMe = xm[i];
}
for (int i = 0; i < 10; i++)
st[i].grades = rand() % 5 + 95;
sort(st, st + 10,compare);//注意这里是st+10不是st+9!!
for (int i = 0; i < 10; i++) {
cout << st[i].NaMe << " " << st[i].grades << endl;
}
cout << endl;
return 0;
}
第二种实现方式:
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstring>
using namespace std;
struct student {
int grades = 0;//初始化成员变量
string NaMe;
}st[10];
bool compare(const student& a, const student& b)
{
if ((a.grades > b.grades) || ((a.grades == b.grades) && (a.NaMe < b.NaMe)))
return true;
else
return false;
}
int main() {
srand((unsigned)time(NULL));
string xm[10] = { "支文栋","党乐章","毛乐家","公冶彭魄","胥建树","禄锐利","周明诚","郝阳云","朱德容","端木和正" };
for (int i = 0; i < 10; i++) {
st[i].NaMe = xm[i];
}
for (int i = 0; i < 10; i++)
st[i].grades = rand() % 5 + 95;
sort(st, st + 10, compare);//注意这里是st+10不是st+9!!
for (int i = 0; i < 10; i++) {
cout << st[i].NaMe << " " << st[i].grades << endl;
}
cout << endl;
return 0;
}
输出如下: