• C++标准模板库(STL)——sort()


    sort是用来排序的函数,效率较高。

    1.如何使用sort函数

    必须加上头文件:"include <algorithm>"和"using namespace;"

    使用方法如下:

    sort(首元素地址(必填),尾元素地址的下一个地址(必填),比较函数(非必填));

    默认对前面的区间进行递增排序;

    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int a[6]={9,4,2,5,6,-1};
        //将a[0]~a[3]从小到大排序
        sort(a,a+4);
        for(int i=0;i<6;i++)
        cout << a[i] << ' ';
        cout << endl;
        
        //将所有数排序
        sort(a,a+6);
        for(int i=0;i<6;i++)
        cout << a[i] << ' ';
        cout << endl; 
        return 0;
    }

     double型数组排序同int型;

    对char型数组排序——默认为字典序

    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        char c[]={'T','W','A','K','I','Y'};
        sort(c,c+6);
        for(int i=0;i<6;i++)
        cout << c[i] << ' ';
        cout << endl;
        return 0;
    }

     注意:如果需要对序列进行排序,那么序列中的元素一定要具有可比性;特别是结构体,需要人为制定比较规则。

    2.如何实现比较函数cmp()——compare函数

    1)基本数据类型数组的排序

    若比较函数不填,则默认从小到大的顺序排列

    如果想要从大到小排列,就需要使用比较函数cmp来告诉sort合适要交换元素。

    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    bool cmp(int a,int b)
    {
        return a>b; //可以理解为当a>b时,将a放在b前 
    }
    
    int main()
    {
        int a[6]={9,4,2,5,6,-1};
        sort(a,a+6,cmp);
        for(int i=0;i<6;i++)
        cout << a[i] << ' ';
        cout << endl; 
        return 0;
    }

     double型,char型数组排序同int型;

    2)结构体数组的排序

    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    struct node{
        int x,y;
    }ssd[10];
    
    bool cmp(node a,node b)
    {
        if(a.x!=b.x)
        return a.x>b.x;
        else   //二级排序:当x相等时按y从大到小排序 
        return a.y>b.y;
    }
    
    int main()
    {
        ssd[0].x=2;
        ssd[0].y=2;
        ssd[1].x=1;
        ssd[1].y=3;
        ssd[2].x=2;
        ssd[2].y=1;
        sort(ssd,ssd+3,cmp);
        for(int i=0;i<3;i++)
        cout << ssd[i].x << ' ' << ssd[i].y << endl; 
        return 0;
    }

     

     3)容器的排序

    在STL标准容器中,只有vector,string,deque可以使用sort的。

    以vector为例:

    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    bool cmp(int a,int b)
    {
        return a>b;
    }
    
    int main()
    {
        vector<int> vi;
        vi.push_back(3);
        vi.push_back(1);
        vi.push_back(2);
        sort(vi.begin(),vi.end(),cmp);  //对整个vector排序
        for(int i=0;i<3;i++)
        cout << vi[i] << ' ';
        cout << endl; 
        return 0;
    }

     string 排序

    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    int main()
    {
        string str[3]={"bbbb","cc","aaa"};
        sort(str,str+3);
        for(int i=0;i<3;i++)
        cout << str[i] << endl;
        return 0;
    }

     字符串长度排序

    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    bool cmp(string a,string b)
    {
        return a.length()<b.length();
    }
    int main()
    {
        string str[3]={"C加加","游戏","编程学习"};
        sort(str,str+3,cmp);
        for(int i=0;i<3;i++)
        cout << str[i] << endl;
        return 0;
    }

  • 相关阅读:
    attr系列保留方法使用
    利用python的标准库hashlib 的md5()生成唯一的id
    【病因】 神经衰弱的几大病因
    群里看到的一个骗子批八字的例子
    i'll make a man out of you
    It's A Good Day To Die
    两天了。照着SVN的界面画的一个界面。
    起一卦,看看我想要的,依然这么倒霉
    倒霉倒霉真倒霉,这一卦起得和上一卦一样
    只要是倒霉,起卦就能看出来
  • 原文地址:https://www.cnblogs.com/wlyperfect/p/12504735.html
Copyright © 2020-2023  润新知