实用sort函数

加粗样式`

1、sort函数的基本使用。

// algorithm 译:算法。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	// 学会使用 sort排序函数
	// 1、将整数排序;
	int a[] = {3, 6, 1, 5, 2, 4};
	int n1 = 6;						// 将该数组中的前n项排序; 
	sort(a,a+n1); 					// sort(首元素地址(必填), 尾元素地址的下一个地址(必填), 比较函数(非必填)): 
	// 因为对其操作的是指针,所以我们不需要返回值,直接打印出来。
	for (int i = 0; i < n1; i++) {
		cout << a[i] << " ";
	} 
	cout << endl;
	// ----------------》在默认前提下,该sort函数是按 递增 排序得到。
	
	// 2、对double排序;
	double b[] = {6.6, -3, 2.6, 9.8}; 
	int n2 = 4;
	sort(b,b + n2);
	for (int i = 0; i < n2; i++) {
		cout << b[i] << " ";
	}
	cout << endl;
	
	// 3、对char型数组排序 (默认按字典顺序) 
	char c[] = {'e','r','u','a'};
	int n3 = 4;
	sort(c,c + n3);
	for (int i = 0; i < n3; i++) {
		cout << c[i] << " ";
	} 
	
	
	// 未完待续... 
	
	
	return 0;
} 

2、使用sort函数进行一些简单的排序。

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
// 1、cmp函数; 
bool cmp1(int a, int b) {
	return a > b;
}
// 2、一般在我们实际做题当中大多会遇到一个结构体,比方说:总分相同的前提下,再按语文成绩排序这样的,cmp函数如下:

struct stu1{
	string name;
	int number;
	int score;
	int chine; 
};
bool cmp2(stu1 a, stu1 b)
{
	if (a.score != b.score)
		return a.score > b.score;
	else
		return a.chine > b.chine;	
}
// 3.用于我们实际生活中的成绩排序; 
struct stu2{
	string name;
	string number;			// 建议这里的学号设成字符串形式,因为这样才更好比较两个大于10位数的学号,因为int型最高是10位。 
	int score;
	int chine; 
	int rank;		// 新加的排名。 
}; 
bool cmp3(stu2 a, stu2 b) {
	if (a.score != b.score)
		return a.score > b.score;
	else
		return a.number.compare(b.number) < 0;	 
} 
int main() 
{
	// 上接1:但是我们如果是想得到从大到小的排序,就要用到 cmp函数,下面我们就来写cmp函数。 
	int a[] = {3, 6, 9, 2}; 
	sort(a, a + 4, cmp1);
	for (int i = 0; i < 4; i++) {
		cout << a[i] << " "; 
	}
	cout << endl;
	// 输出结果: 9 , 6, 3, 2; 
	
	// 2.上接上面结构体题;
	
	stu1 str[] = {{"wen",111,386,88},{"you",222,386,99},{"yang",333,459,99}}; 
	sort(str,str + 3,cmp2);
	for (int i = 0; i < 3; i++) {
	 	cout << str[i].name << " " <<  str[i].number << " " << str[i].score << " " << str[i].chine << endl;
	}
	cout << endl;
	
	// 3.一般我们用在学生成绩的排序上时,会遇到如果两个同学总分相同的前提下,会给两个同学安排一养的名次。
	// 	 当然在此之后的同学 排名还是要靠后的,举个例子:400,369,369,356;最后给出来的排名是400-1;369-2;369-2;356-4;
	//   然后我们可以把排名在定义结构体时就加上去。
	stu2 str2[] = {{"文","111",450, 109}, {"有","222",450,117}, {"梅","333",405,108}}; 
	sort(str2,str2 + 3, cmp3);
	str2[0].rank = 1;
	for (int i = 1; i < 3; i++) {
		if (str2[i].score == str2[i-1].score)
			str2[i].rank = str2[i-1].rank;
		else
			str2[i].rank = i + 1;				// 如果成绩不相等的话就得名次就是i + 1;	
	}
	for (int i = 0; i < 3; i++) {
		cout << str2[i].name << " " <<  str2[i].number << " " << str2[i].score << " " << str2[i].chine << " " << str2[i].rank << endl;
	} 
	
	
	// 这儿还有一种排序方法,不用在定义结构体时加入排名,因为有些时候我们有排名可以输出便可,如下:
	/*
	int r = 1;
	cout << r " ";
	for (int i = 0; i < n; i++) {
		if (str2[i].score != str2[i].score)
			r = i + 1;
		// else	r不变;
		cout << r << " "; 	
	}
	
		
	*/ 
	return 0;
}

谢谢观看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吾独于卿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值