sort排序函数用法总结

注意:使用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;
}

输出如下:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Grausam

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值