zoukankan      html  css  js  c++  java
  • 库函数sort 和 qsort的使用

    #include <iostream>
    #include <algorithm>
    #include <stdlib.h>
    using namespace std;
    
    struct node
    {
        int x;
        int y;
    };
    
    int cmp(const void *a,const void *b)
    {
        return *(int *)a-*(int *)b;
    }//从小到大排
    
    bool comp(int a,int b)
    {
        if(a<b)
            return true;
        return false;
    }//从小到大排
    
    bool cmpp(node a,node b)
    {
        if(a.x<b.x)
            return true;
        else
        {
            if(a.x==b.x)
            {
                if(a.y<b.y)
                    return true;
            }
            return false;
        }
    
        return false;
    }//先按x从小到大排,如果x相等,再按y从小到大排
    
    int main()
    {
        int a[5]={5,4,3,2,1};
        int aa[5]={5,4,3,2,1};
        int b[5]={10,9,8,7,6};
        node s[3]={{1,2},{2,4},{2,3}};
        int i;
        sort(a,a+5);
        sort(aa,aa+5,comp);
        sort(s,s+3,cmpp);
        qsort(b,5,4,cmp);
        for(i=0;i<=4;i++)
            cout<<a[i]<<" ";
        cout<<endl;
    
        for(i=0;i<=4;i++)
            cout<<aa[i]<<" ";
        cout<<endl;
    
        for(i=0;i<=4;i++)
            cout<<b[i]<<" ";
        cout<<endl;
    
        for(i=0;i<=2;i++)
            cout<<s[i].x<<" "<<s[i].y<<endl;
    
    
    
    }
    

    运行截图:



  • 相关阅读:
    hihocoder 1038
    hihocoder 1039
    poj 2774
    bzoj 4690&&4602
    poj 2417
    STL
    poj 1026
    poj 1064
    poj 1861(prim)
    poj 1129
  • 原文地址:https://www.cnblogs.com/sr1993/p/3697826.html
Copyright © 2011-2022 走看看