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

三分钟学会sort排序详解(新手版)C 新生教程 sort 排序

大家好,欢迎来到IT知识分享网。

一.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; } 

运行结果三分钟学会sort排序详解(新手版)

 二.升序降序多种方式

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

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; } 

运行结果

三分钟学会sort排序详解(新手版)与上面是一样的

 对于我们学习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; } 

 运行结果三分钟学会sort排序详解(新手版)

     第二种   

     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; } 

运行结果

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

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

常见特殊例题

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; } 

运行结果

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

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

三.字母按字典排序

         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; } 

运行结果

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

第一行是自己输入的: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; } 

运行结果 

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

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

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/114316.html

(0)
上一篇 2025-12-09 13:15
下一篇 2025-12-09 13:26

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信