C++中的sort()函数

简介

STL中的sort()并非只是普通的快速排序,除了对普通的快速排序进行优化,它还结合了插入排序和堆排序。根据不同的数量级别以及不同情况,能自动选用合适的排序方法。当数据量较大时采用快速排序,分段递归。一旦分段后的数据量小于某个阀值,为避免递归调用带来过大的额外负荷,便会改用插入排序。而如果递归层次过深,有出现最坏情况的倾向,还会改用堆排序。所以说sort()是一个比较灵活的函数,它也会根据我们数据的需要进行排序,所以我们就不用担心以上的问题了。对于大部分的排序需求,sort()都是可以满足的。

头文件

算法头文件

#include<algorithm>

用法

std::sort()函数的常用参数

sort()函数的第一个参数为需要排序的数组或容器的开始迭代器(理解为指针),第二个参数为需要排序的数组或容器的结束迭代器。这两个参数决定了要对哪些元素进行排序。

sort()函数的第三个参数为一个比较函数,用于指定排序的顺序。默认为less<T>(),即升序排序,如果需要降序排序,可以使用greater<T>()

自定义排序准则

使用sort()我们不仅仅可以从大到小排或者从小到大排,还可以按照一定的准则进行排序。

比如说我们按照每个数的个位进行从大到小排序,我们就可以根据自己的需求来写一个函数作为排序的准则传入到sort()中。

我们还可以自行重载编写排序准则,可以看看另一篇关于重载运算符的文章。

我们可以将这个函数定义为:

bool cmp(int x,int y){
	return x % 10 > y % 10;
}

 以下是使用示例:

#include<iostream>
#include<algorithm>
using namespace std;

bool cmp(int x,int y){
	return x % 10 > y % 10;
}

int main(){
	int num[10] = {65,59,96,13,21,80,72,33,44,99};
	sort(num,num+10,cmp);
	for(int i=0;i<10;i++){
		cout<<num[i]<<" ";
	}
	
	return 0;
	
} 
输出结果:59 99 96 65 44 13 33 72 21 80

对数组排序

sort(array, array + n, cmp);

其中array为数组名,n为数组的长度,cmp为比较函数。

以下是一个使用sort()函数对数组进行排序的示例:

#include <iostream> 
#include <algorithm> 
using namespace std;

bool cmp(int a, int b) { 
    return a > b; 
}

int main() { 
    int a[5] = {1, 2, 3, 4, 5}; 
    sort(a, a + 5, cmp); 
    for (int i = 0; i < 5; i++) cout << a[i] << " "; 
    return 0; 
}
输出结果为:5 4 3 2 1

对容器排序

sort(container.begin(), container.end(), cmp);

其中container为容器,cmp为比较函数。

以下是一个使用sort()函数对容器进行排序的示例:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

bool cmp(const string& s1, const string& s2) {
    return s1.size() < s2.size();
}

int main() {
    vector<string> v = {"hello", "world", "this", "is", "a", "test"};
    sort(v.begin(), v.end(), cmp);
    for (auto s : v) cout << s << " ";
    return 0;
}
输出结果为:a is test this world hello

对结构体排序

sort()也可以对结构体进行排序。

比如我们定义一个结构体含有学生的姓名和成绩的结构体Student,然后我们按照每个学生的成绩从高到底进行排序。

再比如每一个学生有四科成绩,我们需要根据学生的四科成绩的平均分高低进行排名。

以下是一个使用sort()函数对结构体进行排序的示例:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

struct Student{
	string name;
	double score[4];
};

bool cmp_score(Student x,Student y){
	double average_x,average_y;
	average_x = (x.score[0]+x.score[1]+x.score[2]+x.score[3])/4;
	average_y = (y.score[0]+y.score[1]+y.score[2]+y.score[3])/4;
	return average_x > average_y;
}

int main(){
	Student stu[3];
	string n;
	int s;
	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_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;
}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hiOoo.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值