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

三分钟学会sort排序详解(新手版)一 sort 基础 1 sort 函数默认的排序方式 是升序排序 即从小到大 2 sort 排序格式 sort begin end cmp sort 函数中参数有三个 对于一般排序第三个可以省略 其中 begin 是排序数组的起始地址 end 是排序数组的结束地址 举例简单 sort 排序 include

大家好,我是讯享网,很高兴认识大家。

一.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', , 1.82, 65.0}, {"Zhangmeng", 'M', , 1.75, 58.0}, {"Helei ", 'M', , 1.83, 67.1}, {"Geyujian ", 'M', , 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; } 

运行结果 

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

小讯
上一篇 2025-02-10 19:03
下一篇 2025-03-19 15:04

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/88259.html