1、sort函数的时间复杂度为n*log2(n),执行效率较高。

2、sort函数的形式为sort(first,end,method)//其中第三个参数可选。

3、若为两个参数,则sort的排序默认是从小到大,见如下例子

  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. int main()
  5. {
  6. int a[10]={9,6,3,8,5,2,7,4,1,0};
  7. for(int i=0;i<10;i++)
  8. cout<<a[i]<<endl;
  9. sort(a,a+10); //可以看出,两个参数为均地址,a为起始,a+10为结束位置
  10. for(int i=0;i<10;i++)
  11. cout<<a[i]<<endl;
  12. return 0;
  13. }
#include<iostream>

#include<algorithm>

using namespace std;

int main()

{

  int a[10]={9,6,3,8,5,2,7,4,1,0};

  for(int i=0;i<10;i++)

  cout<<a[i]<<endl;

  sort(a,a+10); //可以看出,两个参数为均地址,a为起始,a+10为结束位置

  for(int i=0;i<10;i++)

  cout<<a[i]<<endl;

  return 0;

}

4、若为三个参数,则需要写一个cmp函数(此名称cmp可变),用于判断是从小到大排序还是从大到小排序。

(1)需要排序的数组直接为int类型,则见如下例子(从大到小排序)

  1. #include <algorithm>
  2. #include <iostream>
  3. using namespace std;
  4. bool com(int a,int b)
  5. {
  6. return a>b;
  7. }
  8. int main()
  9. {
  10. int a[10]={9,6,3,8,5,2,7,4,1,0};
  11. for(int i=0;i<10;i++)
  12. cout<<a[i]<<endl;
  13. sort(a,a+10,com);//在这里就不需要对com函数传入参数
  14. for(int i=0;i<10;i++)
  15. cout<<a[i]<<endl;
  16. return 0;
  17. }
#include <algorithm>
#include <iostream>
using namespace std; bool com(int a,int b) { return a>b; } int main() { int a[10]={9,6,3,8,5,2,7,4,1,0}; for(int i=0;i<10;i++) cout<<a[i]<<endl; sort(a,a+10,com);//在这里就不需要对com函数传入参数 for(int i=0;i<10;i++) cout<<a[i]<<endl; return 0; }

(2)如果想依照一个结构体内的一个int型的属性参数进行排序,则见如下例子(从大到小排列)

    1. #include <iostream>
    2. #include <algorithm>
    3. using namespace std;
    4. struct node {
    5. int a;
    6. //.........
    7. //
    8. };
    9. bool cmp(node x,node y)
    10. {
    11. if(x.a != y.a)
    12. return (x.a > y.a);
    13. }
    14. void main(void)
    15. {
    16. int i;
    17. node N_t[5];
    18. for(i=0; i<5; i++)
    19. {
    20. cin>>N_t[i].a;
    21. }
    22. sort(N_t, N_t+5, cmp);
    23. for(i=0; i<5; i++)
    24. {
    25. cout<<N_t[i].a;
    26. }
    27. }

#include <algorithm>中sort的一般用法的更多相关文章

  1. <algorithm>中sort()函数的用法

    先说一下,本篇文章我没有讲sort()实现排序的原理,我写在另一篇文章中了,如果想了解的话,可以看一下,附上链接:https://www.cnblogs.com/buanxu/p/12772700.h ...

  2. C++<algorithm>中sort的比较函数写法(转)

    转自:http://www.wl566.com/biancheng/98907.html C++<algorithm>中sort的比较函数写法,有需要的朋友可以参考下. 定义排序函数: 方 ...

  3. JS中sort()方法的用法,参数以及排序原理

    sort() 方法用于对数组的元素进行排序,并返回数组.默认排序顺序是根据字符串Unicode码点.语法:arrayObject.sort(sortby):参数sortby可选.规定排序顺序.必须是函 ...

  4. python中sort和sorted用法的区别

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...

  5. c++中sort基础用法

    用法一:数组排序 对一个数组进行升序排序 #include <algorithm> #include <iostream> #include <cstdio> us ...

  6. STL之sort函数的用法

    说明:本文仅供学习交流,转载请标明出处,欢迎转载! STL封装了一个排序算法,该算法相应的头文件为#include<algorithm>,我们能够依据须要对一个数组进行排序或者降序. so ...

  7. 转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    转载自:http://www.cnblogs.com/cj695/p/3863142.html sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在 ...

  8. 【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...

  9. C++ 中的sort()排序函数用法

    sort(first_pointer,first_pointer+n,cmp) 该函数可以给数组,或者链表list.向量排序. 实现原理:sort并不是简单的快速排序,它对普通的快速排序进行了优化,此 ...

随机推荐

  1. 使用autoconfig

    1. 增加maven 依赖 <properties> <maven.compiler.target>1.8</maven.compiler.target> < ...

  2. js-取值&赋值-获取某标签某属性的值

      js 取值&赋值-获取某标签某属性的值 CreateTime--2016年10月16日16:35:34 Author:Marydon 1.取值 //方法一 //自定义属性必须用getAtt ...

  3. 金山PDF

    金山是个很不错的软件公司,金山出PDF,纯粹是完善生态圈!毕竟没FoxitReader专业对PDF的处理上! 官网:芝麻开门 下载:http://wdl1.cache.wps.cn/wps/downl ...

  4. set 容器 的全解(转)

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  5. php 回收周期(Collecting Cycles)

    http://docs.php.net/manual/zh/features.gc.collecting-cycles.php 传统上,像以前的 php 用到的引用计数内存机制,无法处理循环的引用内存 ...

  6. RabbitMQ消息队列(六):使用主题进行消息分发[转]

    在上篇文章RabbitMQ消息队列(五):Routing 消息路由 中,我们实现了一个简单的日志系统.Consumer可以监听不同severity(严重级别)的log.但是,这也是它之所以叫做简单日志 ...

  7. iOS - AsyncSocket 的使用

    1.AsyncSocket 基于 CFSocket.GCD 进行的封装(OC). 支持 TCP 和 UDP. 完整的回调函数(用于处理各种回调事件,连接成功,断开连接,收到数据等). 需要注意的问题: ...

  8. RabbitMQ与.net core(五) topic类型 与 headers类型 的Exchange

    1.topic类型的Exchange 我们之前说过Topic类型的Exchange是direct类型的模糊查询模式,可以通过routkey来实现模糊消费message,topic的模糊匹配有两种模式: ...

  9. hello oc

    printf("Hello C\n"); //OC可以采用C语言的输出方式 printf("The number is %d\n",100);//%d 输出数字 ...

  10. 摘:ClickOnce部署

    ClickOnce部署 http://www.cnblogs.com/weixing/p/3358740.html#undefined (1):一些发布方式 ClickOnce是什么玩意儿,这个问题嘛 ...