zoukankan      html  css  js  c++  java
  • C++ STL之排序算法

    排序算法和查找算法差不多,也涉及到迭代器区间问题,关于该问题的注意事项就不在啰嗦了

    一、全部排序sort、stable_sort

    sort是一种不稳定排序,使用时需要包含头文件algorithm

    默认可以传两个参数或三个参数。第一个参数是要排序的区间首地址,第二个参数是区间尾地址的下一地址。如果只传入这两个地址的话,就按照升序对指定地址区间排序。想要按照降序排列的话,需要传入第三个函数,第三个函数可以自己写cmp,也可以直接调用库函数

    greater<data-type>(),使用库函数的时候要包含头文件functional。

    总结如下:

    #include<algorithm>

    #include<functional>

    升序:sort(begin,end,less<data-type>());

    降序:sort(begin,end,greater<data-type>()).

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<functional>
     4 using namespace std;
     5 int main()
     6 {
     7     int a[9]={454,45564,6,3,5,65,32,53,5};
     8     //升序
     9     sort(a,a+9,less<int>());
    10     cout<<"升序排序结果"<<endl;
    11     for(int i=0;i<9;i++)
    12     {
    13         cout<<a[i]<<endl;
    14     }
    15     //降序
    16     sort(a,a+9,greater<int>());
    17     cout<<"降序排序结果"<<endl;
    18     for(int i=0;i<9;i++)
    19     {
    20         cout<<a[i]<<endl;
    21     }
    22     return 0;
    23 }

    如果忘记了升序或者降序后面的那个方法名称,也可以自己写个简单的

    bool cmp(int a,int b)
    {
     return a>b;
    }

    sort(a,a+9,cmp);
    就是降序

    bool cmp(int a,int b)

    {

    return a<b;

    }

    sort(a,a+9,cmp);

    就是升序

    与之对应的有一个stable_sort()用法与sort一样,是稳定排序。

    二、部分排序partial_sort、partial_sort_copy

     1 #include<iostream>
     2 #include<algorithm>
     3 using namespace std;
     4 int main()
     5 {
     6     int num[10]={3,4,6,7,3,5,2,9,7,8};
     7     for(int i=0;i<10;i++)
     8     {
     9         cout<<num[i]<<" ";
    10     }
    11     cout<<endl;
    12     //partial_sort
    13     //void partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last, _Pr _Pred)
    14     //选出[_First, _Last)之间的_Mid-_First个数进行排序,放在_First到_Mid位置,剩下的在_Mid到_Last的元素不排序,pred是排序方式
    15     partial_sort(num,num+3,num+10);
    16     for(int i=0;i<10;i++)
    17     {
    18         cout<<num[i]<<" ";
    19     }
    20     cout<<endl;
    21 
    22     //partial_sort_copy
    23     //_RanIt partial_sort_copy(_InIt _First1, _InIt _Last1,_RanIt _First2, _RanIt _Last2, _Pr _Pred)
    24     int shuzi[10]={3,4,6,7,3,5,2,9,7,8};
    25     int result1[3];
    26     int result2[12];
    27     partial_sort_copy(shuzi,shuzi+10,result1,result1+3);
    28     for(int i=0;i<10;i++)
    29     {
    30         cout<<shuzi[i]<<" ";
    31     }
    32     cout<<endl;
    33     for(int i=0;i<3;i++)
    34     {
    35         cout<<result1[i]<<" ";
    36     }
    37     cout<<endl;
    38         
    39     partial_sort_copy(shuzi,shuzi+10,result2,result2+12);
    40     for(int i=0;i<10;i++)
    41     {
    42         cout<<shuzi[i]<<" ";
    43     }
    44     cout<<endl;
    45     for(int i=0;i<12;i++)
    46     {
    47         cout<<result2[i]<<" ";
    48     }
    49     cout<<endl;
    50     return 0;
    51 }
  • 相关阅读:
    动态代理练习2全站压缩流输出[response动态代理]
    动态代理练习3自定义数据库连接池[connection动态代理]
    类加载器
    Mysql中的数据类型对应于Java中什么数据类型
    使用网上流传的一个数据库连接池在Proxy.newProxyInstance处引起 java.lang.ClassCastException 问题的解决方法
    动态代理练习1全站字符编码过滤[request动态代理]
    用cocos2dx将helloworld实现出来了
    多线程的自动管理(线程池)
    互斥对象
    多线程的自动管理(定时器)
  • 原文地址:https://www.cnblogs.com/bewolf/p/4378054.html
Copyright © 2011-2022 走看看