基于C++标准模板库(STL)的sort排序函数,超实用介绍!!!

sort函数

简介:顾名思义,sort就是用来排序的函数,它可以根据具体情形进行自动或人为使用不同得排序方法,接下来希望通过这篇介绍来帮助读者们轻松愉快地使用sort函数

1.如何使用sort排序

使用条件:

sort函数的使用必须加上头文件#include<algorithm>和using namespace std;

使用格式:

sort(首元素地址(必填),尾元素地址的下一个地址(必填),比较函数(选填));

注意事项:

从使用格式上,我们可以发现sort函数前两个是必填的,而比较函数是选填的,因此如果不写比较函数的话,将默认该排序方法是递增排序

代码示例:
#include<stdio.h>
#include<algorithm>
using namespace std;

int main(){
	
	int a[6] = {8,6,4,1,3,9};
	//将a[0]~a[3]从小到大排序
	sort(a,a+4);
	for(int i = 0;i < 6;i++){
		printf("%d",a[i]);
	}
	return 0;
}

运行结果:
1 4 6 8 3 9

对于字符数组的排序,默认为字典序:

#include<stdio.h>
#include<algorithm> 
using namespace std;

int main(){
	
	char a[] = {'K','H','T','A'};
	sort(a,a+4);
	for(int i = 0;i < 4;i++){
		printf("%c",a[i]);
	}
	return 0;
} 

运行结果:
AHKT

2.如何实现比较函数cmp

编写cmp比较函数:接下来将会介绍对基本数据类型、结构体类型和STL个别容器进行自定义规则排序的cmp写法

  • 不用cmp函数时
#include<stdio.h>
#include<algorithm>
using namespace std;

int main(){
	
	int a[6] = {8,6,4,1,3,9};
	//将a[0]~a[6]从小到大排序
	sort(a,a+6);
	for(int i = 0;i < 6;i++){
		printf("%d",a[i]);
	}
	return 0;
}

运行结果:
1 3 4 6 8 9
  • 使用cmp函数
#include<stdio.h>
#include<algorithm>
using namespace std;

bool cmp(int a,int b){
	return a > b;//意:设定排序规则为从大到小排序 
}

int main(){
	
	int a[6] = {8,6,4,1,3,9};	
	sort(a,a+6,cmp);//使用cmp函数
	for(int i = 0;i < 6;i++){
		printf("%d",a[i]);
	}
	return 0;
}


运行结果:
9 8 6 4 3 1

对于char型数组从大到小排序

  • 不用cmp函数时
#include<stdio.h>
#include<algorithm> 
using namespace std;

int main(){
	
	char a[] = {'K','H','T','A'};
	sort(a,a+4);
	for(int i = 0;i < 4;i++){
		printf("%c",a[i]);
	}
	return 0;
} 

运行结果:
AHKT
  • 使用cmp函数
#include<stdio.h>
#include<algorithm> 
using namespace std;

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

int main(){
	
	char a[] = {'K','H','T','A'};
	sort(a,a+4,cmp);
	for(int i = 0;i < 4;i++){
		printf("%c",a[i]);
	}
	return 0;
} 

运行结果:
TKHA

案例说明
通过对int型和char型数组的举例,我们可以发现

如果要把数据从大到小进行排列的话,cmp函数中是使用">"

反之,若想进行从小到大排列的话则是使用"<"。

当不太记得规则时我们只需实操实验观察,便可知道了。

cmp函数注意事项
①函数类型为bool型
②函数的参数类型与排序的数据的类型相同
③设定规则:return + “>”(递减) or “<”(递增);

对于结构体数组的排序
  • 案例一
#include<stdio.h>
#include<algorithm> 
using namespace std;

struct node{
	int x,y;
}pt[10];

bool cmp(node a,node b){
	return a.x > b.x;
}

int main(){
	/*
	pt[0]:(2,2)
	pt[1]:(1,5)
	pt[2]:(3,2)
	通过比较x的大小来进行递减排序	 
	*/
	pt[0].x = 2;
	pt[0].y = 2;
	pt[1].x = 1;
	pt[1].y = 5;
	pt[2].x = 3;
	pt[2].y = 2;
	sort(pt,pt+3,cmp);
	for(int i = 0;i < 3;i++){
		printf("%d %d\n",pt[i].x,pt[i].y);
	}
	return 0;
} 

运行结果:
3 2
2 2
1 5

  • 案例二
#include<stdio.h>
#include<algorithm> 
using namespace std;

struct node{
	int x,y;
}pt[10];

bool cmp(node a,node b){
	if(a.x == b.x) return a.y < b.y; 
	else return a.x > b.x;
}

int main(){
	/*
	pt[0]:(2,2)
	pt[1]:(2,5)
	pt[2]:(3,2)
	通过比较x的大小来进行递减排序
	当x相同时比较y,y采用递增排序	 
	*/
	pt[0].x = 2;
	pt[0].y = 2;
	pt[1].x = 2;
	pt[1].y = 5;
	pt[2].x = 3;
	pt[2].y = 2;
	sort(pt,pt+3,cmp);
	for(int i = 0;i < 3;i++){
		printf("%d %d\n",pt[i].x,pt[i].y);
	}
	return 0;
} 

运行结果:
3 2
2 2
2 5

对于其它容器的排序(小扩展)

基于vector的排序:

#include<stdio.h>
#include<vector> 
#include<algorithm> 
using namespace std;

bool cmp(int a,int b){
	
	return a > b;//从大到小 
}

int main(){
	vector <int> v;
	v.push_back(5);
	v.push_back(8);
	v.push_back(23);
	v.push_back(1);
	sort(v.begin(),v.end(),cmp);
	for(int i = 0;i < 4;i++){
		printf("%d ",v[i]);
	}
	return 0;
} 

运行结果:
23 8 5 1
  • 基于string型字符串的长度大小比较
#include<iostream>
#include<string> 
#include<algorithm> 
using namespace std;

bool cmp(string a,string b){
	if(a.length() == b.length()) return a < b;//如果长度相等,则根据字典序进行排序 
	else return a.length() > b.length();//从大到小 
}

int main(){
	string str[4] = {"dada","fff","eee","fas"};
	sort(str,str+4,cmp);
	for(int i = 0;i < 4;i++){
		cout<<str[i]<<endl;
	}
	return 0;
} 

运行结果:
dada
eee
fas
fff

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值