三分钟学会sort排序详解(新手版)

一.sort基础

                  1.sort函数默认的排序方式是升序排序,即从小到大。  

                  2.sort排序格式:sort(begin,end,cmp); (sort函数中参数有三个(对于一般排序第三个可以省略) 其中begin是排序数组的起始地址, end是排序数组的结束地址

                举例简单sort排序

#include <iostream>
#include<algorithm>

using namespace std;

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

运行结果

 二.升序降序多种方式

除了默认排序外还有很多种排序方式

1.升序方式:

  cmp函数:             !!!这串代码放在int main()之前

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

实例,在上面代码基础上修改一下

#include <iostream>
#include<algorithm>

using namespace std;
bool cmp(int a,int b)
{
    return a<b;
}

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

运行结果

与上面是一样的

 对于我们学习C++语言来说,只会升序是不够的还要会降序按照字典排序,

2.降序方式:
          第一种 

          改变上面cmp函数返回值的小于符号为大于符号

改了之后如下

#include <iostream>
#include<algorithm>

using namespace std;
bool cmp(int a,int b)
{
    return a>b;                //改“a<b”为“a>b", 
}

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

 运行结果

     第二种   

     greater函数:
           格式:

sort(a,a+6,greater<int>());

greater是已经给定可以直接使用的排序函数,自己用,不需要定义

#include <iostream>
#include<algorithm>

using namespace std;
int main()
{
    int a[6]={3,5,8,6,2,9};
    sort(a,a+6,greater<int>());
    for(int i=0;i<6;i++)
        cout<<a[i]<<" ";
    return 0;
}

运行结果

升序,降序函数有很多,掌握这两个基本能够完成大部分任务, 

常见特殊例题

cmp特殊用法,

例题,现在有一组数字为两位数,要按每个数的个位数从大到小排序(从小到大)

#include <iostream>
#include<algorithm>
using namespace std;
bool cmp(int x, int y)
{
	return x % 10 > y % 10;
}
int main()
{
	int a[5] = { 23,19,54,16,17 };
	sort(a, a + 5, cmp);
	for (int i = 0; i < 5; i++)
		cout << a[i] << ' ';
	return 0;
}

运行结果

从小到大,同理,改一下符号即可

三.字母按字典排序

         string函数

                   格式:

sort(str.begin(),str.end());

完整代码

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    string str;
    cin>>str;
    sort(str.begin(),str.end());
    cout<<str;
    return 0;
}

运行结果

第一行是自己输入的:shangdong

第二行为结果 

四.结构体的按字母排序排序

       (结构体排序是最难的,同时也是可以让你代码提升一个阶梯对大招)

         没有学过结构体的先去看看结构体,不然看不懂

先上代码

#include <iostream>
#include <cstring>
#include <memory.h>	
#include <algorithm>
using namespace std;
struct student
{
	char name[20];    
	char gender;
	unsigned long birthday;
	double height;
	double weight;
};
int cmp(student a,student b)
{
	return strcmp(b.name,a.name)>0;         //strcmp函数,让首字母按字典排序,升序降序改变符号即可 
}
 student room[4] = {
		{"Lixin    ", 'M', 19980301, 1.82, 65.0},
		{"Zhangmeng", 'M', 19980902, 1.75, 58.0},
		{"Helei    ", 'M', 19981203, 1.83, 67.1},
		{"Geyujian ", 'M', 19980101, 1.70, 59.0}
};				

int main()
{
sort(room,room+4,cmp);              //引用cmp函数 
	for (int i = 0; i < 4; i ++)
	{
		cout<< room[i].name << "\t"
			<< room[i].gender << "\t"
			<< room[i].birthday << "\t"
			<< room[i].height << "\t"
			<< room[i].weight << "\n";
	}

	return 0;
}

运行结果 

我们的看到同学的第一个字母按字典排序了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值