zoukankan      html  css  js  c++  java
  • #include <algorithm>中sort的一般用法

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

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

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

    1. #include<iostream>  
    2.   
    3. #include<algorithm>  
    4.   
    5. using namespace std;  
    6.   
    7. int main()  
    8.   
    9. {  
    10.   
    11.   int a[10]={9,6,3,8,5,2,7,4,1,0};  
    12.   
    13.   for(int i=0;i<10;i++)  
    14.   
    15.   cout<<a[i]<<endl;  
    16.   
    17.   sort(a,a+10); //可以看出,两个参数为均地址,a为起始,a+10为结束位置  
    18.   
    19.   for(int i=0;i<10;i++)  
    20.   
    21.   cout<<a[i]<<endl;  
    22.   
    23.   return 0;  
    24.   
    25. }  
    #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.   
    5. bool com(int a,int b)  
    6.   
    7. {  
    8.   
    9.  return a>b;  
    10.   
    11. }  
    12.   
    13. int main()  
    14.   
    15. {  
    16.   
    17.  int a[10]={9,6,3,8,5,2,7,4,1,0};  
    18.   
    19.  for(int i=0;i<10;i++)  
    20.   
    21.  cout<<a[i]<<endl;  
    22.   
    23.  sort(a,a+10,com);//在这里就不需要对com函数传入参数  
    24.   
    25.  for(int i=0;i<10;i++)  
    26.   
    27.  cout<<a[i]<<endl;  
    28.   
    29.  return 0;  
    30.   
    31. }  
    #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.   
      4. using namespace std;  
      5.   
      6. struct node {  
      7.     int a;  
      8.     //.........  
      9.     //  
      10. };  
      11.   
      12. bool cmp(node x,node y)  
      13. {  
      14.      if(x.a != y.a)   
      15.          return (x.a > y.a);  
      16. }  
      17.   
      18. void main(void)  
      19. {  
      20.     int i;  
      21.     node N_t[5];  
      22.     for(i=0; i<5; i++)  
      23.     {  
      24.         cin>>N_t[i].a;  
      25.     }  
      26.     sort(N_t, N_t+5, cmp);  
      27.     for(i=0; i<5; i++)  
      28.     {  
      29.         cout<<N_t[i].a;  
      30.     }  
      31.       
      32. }     
  • 相关阅读:
    Android 之 JSON操作
    android 之 XMLPull
    DOM 之 SAX操作
    android之DOM生成与解析
    parseDouble()方法
    读取遥感图像中遇到的问题集锦
    xml学习
    linux基础
    hadoop的基本概念 伪分布式hadoop集群的安装 hdfs mapreduce的演示
    【原】自定义tableViewCell的两种方法
  • 原文地址:https://www.cnblogs.com/to-creat/p/5002936.html
Copyright © 2011-2022 走看看