C++ sort()函数

sort() 函数是C++排序方法之一,它的实现方式是基于快排的,所以时间复杂度为 O(nlogn),执行效率较高。

sort() 函数的头文件:#include <algorithm>

sort() 函数的参数:

(1)参数一是要排序的数组的起始地址。

(2)参数二是结束的地址(最后一位要排序的地址)。

(3)参数三是排序的方法,可以是从大到小也可是从小到大,若不写第三个参数,则默认的排序方法是从小到大。

sort() 函数使用模板:sort(start, end, 排序方法);

默认方式排序

#include<iostream>
#include<algorithm>
using namespace std;int main()
{int a[10] = {5,4,3,2,1,6,7,10,9,8};for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a,a+10);cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0;
}

根据排序需求自定义 compare()

bool compare(int a,int b)  //从大到小
{return a > b;
}bool compare(int a, int b)  //绝对值排序
{return abs(a) > abs(b);
}
#include<iostream>
#include<algorithm>using namespace std;bool compare(int a,int b)
{return a>b;
}int main()
{int a[10]={5,4,3,2,1,6,7,10,9,8};for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a, a+10, compare);//不需要对compare函数传入参数cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0;
}

使用参数来确定排序方式

less<数据类型>()     //从小到大排序
greater<数据类型>()  //从大到小排序
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;int main()
{int a[10]={5,4,3,2,1,6,7,10,9,8};for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a, a+10, less<int>());cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0;
}
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;int main()
{int a[10]={5,4,3,2,1,6,7,10,9,8};for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a, a+10, greater<int>());cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0;
}

对字符排序

#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;int main()
{char a[11]="asdfghjklk";for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";sort(a,a+10,greater<char>());cout <<endl;for(int i=0; i<10; i++)cout<< "[" <<a[i] << "]";return 0;
}

对区间内的元素进行排序

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;bool myfunction (int i,int j) { return (i<j); }//升序排列
bool myfunction2 (int i,int j) { return (i>j); }//降序排列struct myclass {bool operator() (int i,int j) { return (i<j);}
} myobject;int main () {int myints[8] = {32,71,12,45,26,80,53,33};vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33//using default comparison (operator <):sort(myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33//using function as compsort(myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)//sort (myints,myints+8,myfunction);不用vector的用法//using object as compsort(myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)//print out content:cout << "myvector contains:";for(vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) //输出cout << ' ' << *it;cout <<endl;return 0;
}

string使用反向迭代器来完成逆序排列

#include <iostream>
#include<algorithm>
using namespace std;int main()
{string str("qweasdzxc");string s(str.rbegin(),str.rend());cout << s <<endl;return 0;
}

参考:https://blog.csdn.net/w_linux/article/details/76222112

https://baike.baidu.com/item/sort%E5%87%BD%E6%95%B0/11042699?fr=aladdin

C++ sort()函数的使用相关推荐

  1. C++ algorithm的sort函数总结

    sort函数 sort对给定区间进行排序,支持各种数据类型,迭代器,结构体,自定义排序规则 stable_sort 对给定区间进行稳定排序,且可保证相等元素的原本相对次序在排序后保持不变 partia ...

  2. C中的qsort函数和C++中的sort函数的理解与使用

    一.qsort()函数 原型:_CRTIMP void __cdecl qsort (void*, size_t, size_t,int (*)(const void*, const void*)); ...

  3. C++ STL的sort 函数 以及自定义的比较函数

    没什么特别擅长的内容,先做个小笔记好了.在编程时,使用C++的标准模板库(STL)能节约工作量,增加代码的可读性,能灵活运用无疑会提高编程的效率,俗话说:Write less, create more ...

  4. 不可不知的STL sort函数实现原理

    sort函数一直以来被认为是快排,今天看到一篇文章,感觉自己知道的太少. 建议大家还是要去啃<STL源码剖析>,我也要去读了,先立个flag,后续1-2个月写STL源码剖析上得到的启发. ...

  5. python sort函数返回值_lambda函数与箭头函数在集合内置函数应用中的对照学习

    Python语言中有一个定义轻量级规则的lambda函数,其语法格式为: Lambda 参数列表:返回值表达式 简单的例子如:定义func=lambda x,y:x+y,则调用func(10,20)的 ...

  6. qsort函数和sort函数

      做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的O(n^2)排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错.STL里面有个sort函数,可以直接对数组排序 ...

  7. python sort函数时间复杂度_STL用法及其时间复杂度分析

    STL是C++语言中一个非常实用的代码库,叫做标准模板库,通常我们使用这个头文件即可导入STL.本文立足与C++,但是python其实也是大同小异. set set正如其名,表示的是一个集合,其分为两 ...

  8. sort函数pythonreverse_Python基础 7 ---- Python内置sort和sorted函数

    1 Python对数据的排序有两种方法,一种是容器内置的sort函数,另外一种利用sorted函数 2 对于sort函数我们不再进行讨论,只要研究一下sorted函数 3 sorted函数的原形sor ...

  9. python:数组/列表(remove()函数、append()函数、sort()函数、reverse()函数)

    排序: 1:整理顺序 #冒泡 lista = [5,7,11,19,99,63,3,9,1] list = [] while lista != []:number = 0for i in lista: ...

  10. STL sort()函数详解

    西方有句谚语:不要重复发明轮子! STL几乎封装了所有的数据结构中的算法,从链表到队列,从向量到堆栈,对hash到二叉树,从搜索到排序,从增加到删除......可以说,如果你理解了STL,你会发现你已 ...

最新文章

  1. 畅想下计算机/计算型设备的未来
  2. java 排队任务_android实现排队任务
  3. Flink DDL的java代碼中的DeserializationSchemaFactory與TableSourceFactory報錯解決方案記錄彙總
  4. 操作系统:UNIX、Linux、IOS、Android之间的渊源
  5. C# 6.0语法新特性体验(二)
  6. VMware vSphere Client客户端安装图解教程
  7. [导入]在asp.net中实现观察者模式,或有更好的方法(续)
  8. 面向兑现编程(四):多态
  9. python用什么来写模块-用Python编写模块有何技巧?
  10. nodeMCU(ESP8266)和RC522的接线图
  11. 网络代理服务器工作原理
  12. 微信公众号查询粉丝列表
  13. 学报格式和论文格式一样吗_工大学报论文格式要求
  14. 微信登录不上显示白屏_微信授权页面在某些手机上为白屏是怎么回事?
  15. poodle attack
  16. (课程笔记)| 林轩田机器学习基石入门(一)
  17. 质数与合数系列——素数判断
  18. 从档案信息管理到档案知识管理
  19. 电脑安装不了pr提示不满足系统要求怎么办?
  20. Nginx HTTP请求的11个阶段

热门文章

  1. 对现有的所能找到的DDOS代码(攻击模块)做出一次分析----GET篇
  2. 高可用系统设计 | 分布式限流策略:计数器算法、漏桶算法、令牌桶算法
  3. Git/SQL/正则表达式练习平台
  4. 马斯克称曾试图将特斯拉出售给苹果、Telegram 用户近5亿、Vimeo将上市等|Decode the Week...
  5. TRTC助力高并发、高可用实时音视频互动场景落地(内含开发福利)
  6. 揭秘熊猫TV HEVC直播
  7. 双双拿下赛道全部指标最佳 | 腾讯新一代VAV1携手V265亮相MSU编码器大赛
  8. GIAC 2020 全球互联网架构大会演讲实录:基于TarsGo的微服务技术架构实践
  9. 新一代海量数据搜索引擎 TurboSearch 来了!
  10. 我眼中的 Nginx(六):深入 Nginx/Openresty 服务里的 DNS 解析