sort函数自定义排序

sort函数介绍

sort()是C++标准库中的排序函数,对给定区间中所有元素进行排序。调用sort()需要的头文件:

#include<algorithm>

using namespace std;

sort()函数采用的是一种类似于快排的排序方式,时间复杂度为n*{log}_2(n),执行效率较高

sort()函数使用方法

sort()函数默认是按升序排序,可以对浮点数、整数、字符、字符串、结构体进行排顺序

排序方法不仅局限于从小到大排序和大到小排序,它可以根据使用者的排序规则进行排序

sort()函数有3个参数:

sort(start, end, cmp);

start和end表示所要排序数组的起始和结束地址,注意是左闭右开,即对区间[start,end)中的元素进行排序;第三个参数为cmp,表示排序的方式,对[start,end)区间中的元素根据cmp来进行排序,也可以不写第三个参数,此时按默认排序,从小到大进行排序

 使用cmp()函数来自己定义排序方式:cmp()函数的返回值一定是bool类型的值,核心之处也是比较,下面举例自定义排序:

1.默认的排序方式

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

int a[10]={7,8,4,0,5,1,3,9,6,2};
int main()
{
	sort(a,a+10);
	for(int i=0;i<10;i++) cout<<a[i]<<" ";
	return 0;
}

输出结果:0 1 2 3 4 5 6 7 8 9

 可以看到sort()是默认从小到大排序的

2.对数组进行自定义排序(从大到小)

#include<algorithm>
using namespace std;
bool cmp(int x,int y)//从大到小进行排序
{
	return x>y;
}
int a[10]={7,8,4,0,5,1,3,9,6,2};
int main()
{
	sort(a,a+10,cmp);
	for(int i=0;i<10;i++) cout<<a[i]<<" ";
	return 0;
}

输出结果:9 8 7 6 5 4 3 2 1 0

2.对字符进行排序

#include<iostream>
#include<algorithm>
using namespace std;
char s[55],t[55];
bool cmp(char a,char b)
{
    return a>b;
}

int main()
{
    int n;
    cin>>n;
    cin>>s;
    sort(s,s+n);
    cout<<s<<endl;    
    sort(s,s+n,cmp);
    cout<<s<<endl;
    return 0;
}

输入:

5
abdce

输出结果:

abcde
edcba

可以看到,sort()对字符的排序是默认按照字典序从小到大进行排列的

3.对字符串进行排序

sort()对字符串排序只能是C++中string类型

#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1e5+10;
string s[maxn];
bool cmp(string a,string b)
{
    return a>b;
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++) cin>>s[i];
    
    sort(s,s+n);
    for(int i=0;i<n;i++) cout<<s[i]<<" ";
    cout<<endl;
    
    sort(s,s+n,cmp); 
    for(int i=0;i<n;i++) cout<<s[i]<<" ";
    cout<<endl;
    
    return 0;
}

输入:

5
aabb
aaaa
aaaaa
ccdd
bbbb

输出结果:
aaaa aaaaa aabb bbbb ccdd
ccdd bbbb aabb aaaaa aaaa
 

可以看到,sort()函数默认以字符串中第一个字符的字典序从小到大进行排序,若第一个字符相同,则以第二个字符的字典序从小到大进行排序,若第二个字符相同,则以第三个字符的字典序按照相同的方法进行排序,以此类推;若两个字符串前面的字符都相同,而长度不同,例如上面例子中的"aaaa"和"aaaaa",则按照字符串短<长的顺序进行排序

3.对结构体进行排序

对结构体按照成员x从小到大排序,若成员x的值相同,就按照成员y从大到小排序,若成员y的值相同,就按照成员z从小到大排序,若x,y,z都相同,按照原本的顺序排列

#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    int x;
    int y;
    int z;
};
bool cmp(node a,node b)
{
	if(a.x != b.x) return a.x < b.x;
	if(a.y != b.y) return a.y > b.y;
	if(a.z != b.z) return a.z < b.z;
	return true;
}
int main()
{
	node ans[5];
	for(int i=0;i<5;i++)
	{
		cin>>ans[i].x>>ans[i].y>>ans[i].z;
	}
	sort(ans,ans+5,cmp);
	for(int i=0;i<5;i++) 
		cout<<ans[i].x<<" "<<ans[i].y<<" "<<ans[i].z<<endl;
	return 0;
}

输入:

2 1 3
1 1 3
1 1 2
1 2 3
7 5 2

输出结果:

1 2 3
1 1 2
1 1 3
2 1 3
7 5 2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值