• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

sort函数

武飞扬头像
柒笙歌
帮助1

目录

sort介绍

sort中cmp的使用

代码样例

输出结果

cmp进行升序排序

代码样例

输出结果

借助less和greater进行升降排序

代码样例

输出结果


下面我来介绍sort的一些用法。

sort介绍

sort(v.begin(),v.end()),它是用来对一组序列进行排序的。sort函数进行排序的时间复杂度为n*log2n,比冒泡之类的排序算法效率要高,包含在头文件为#include<algorithm>的c 标准库中。sort()  函数,本质就是一个模板函数,该函数专门用来对容器或普通数组中指定范围内的元素进行排序,排序规则默认以元素值的大小做升序排序。

    sort()函数除了可以对int型、char型、double型、字符串排序外,还可以实现对结构体、pair、vector等类型进行排序,但需要自己写比较函数。而且sort()既可以对数组排序,也可以对vector容器排序。

sort中cmp的使用

上面说过了sort默认是升序排序,但是如何进行降序排序呢,要想使用sort进行升序排序只需要使用cmp函数就行了,最重要的是,cmp不仅可以进行升序也能进行降序。

代码样例

  1.  
    #include<iostream>
  2.  
    #include<algorithm>
  3.  
    #include<bits/stdc .h>
  4.  
    using namespace std;
  5.  
    bool cmp(int a,int b){
  6.  
    return a>b;
  7.  
    }
  8.  
    int main(){
  9.  
    int str[]={5,3,9,1,5,9,3,0};
  10.  
    int n=sizeof(str)/sizeof(int);
  11.  
    sort(str,str n);
  12.  
    cout<<"sort排序后的结果:";
  13.  
    for(int i=0;i<n;i ){
  14.  
    cout<<str[i]<<" ";
  15.  
    }
  16.  
    cout<<endl<<"使用cmp后的sort的排序结果:" ;
  17.  
    sort(str,str n,cmp);
  18.  
    for(int i=0;i<n;i ){
  19.  
    cout<<str[i]<<" ";
  20.  
    }
  21.  
    return 0;
  22.  
    }
学新通

输出结果

  1.  
    sort排序后的结果:0 1 3 3 5 5 9 9
  2.  
    使用cmp后的sort的排序结果:9 9 5 5 3 3 1 0

cmp进行升序排序

代码样例

  1.  
    #include<iostream>
  2.  
    #include<algorithm>
  3.  
    #include<bits/stdc .h>
  4.  
    using namespace std;
  5.  
    bool cmp(int a,int b){
  6.  
    return a<b;
  7.  
    }
  8.  
    int main(){
  9.  
    int str[]={5,3,9,1,5,9,3,0};
  10.  
    int n=sizeof(str)/sizeof(int);
  11.  
    sort(str,str n,cmp);
  12.  
    cout<<"sort升序后的结果:";
  13.  
    for(int i=0;i<n;i ){
  14.  
    cout<<str[i]<<" ";
  15.  
    }
  16.  
    return 0;
  17.  
    }
学新通

输出结果

sort升序后的结果:0 1 3 3 5 5 9 9

借助less和greater进行升降排序

借助c 标准库来实现降序(或升序)。此时要包含头文件<functional>,<functional>头文件提供了一些基于模板的比较函数对象,这里在排序的时候只用到了 greater<type>() 和 less<type>() 两个;让 greater<type>() 或 less<type>() 做sort()函数的第三个参数来实现升序或降序排列;其中greater<type>() 用于降序排列,less<type>() 用于升序排列

代码样例

  1.  
    #include<iostream>
  2.  
    #include<algorithm>
  3.  
    #include<bits/stdc .h>
  4.  
    using namespace std;
  5.  
    bool cmp(int a,int b){
  6.  
    return a>b;
  7.  
    }
  8.  
    int main(){
  9.  
    int str[]={5,3,9,1,5,9,3,0};
  10.  
    int n=sizeof(str)/sizeof(int);
  11.  
    sort(str,str n,less<int>());
  12.  
    cout<<"sort升序后的结果:";
  13.  
    for(int i=0;i<n;i ){
  14.  
    cout<<str[i]<<" ";
  15.  
    }
  16.  
    cout<<endl<<"sort降序后的结果:" ;
  17.  
    sort(str,str n,greater<int>());
  18.  
    for(int i=0;i<n;i ){
  19.  
    cout<<str[i]<<" ";
  20.  
    }
  21.  
    return 0;
  22.  
    }
学新通

输出结果

  1.  
    sort升序后的结果:0 1 3 3 5 5 9 9
  2.  
    sort降序后的结果:9 9 5 5 3 3 1 0

 关于sort函数的介绍就到这了,希望对大家有用,谢谢大家。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiiiig
系列文章
更多 icon
同类精品
更多 icon
继续加载