1. 程式人生 > >list帶引數的sort函式

list帶引數的sort函式

#include<iostream>
#include<set>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<cmath>
#include<climits>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
typedef long long LL;
using namespace std;
struct node
{
    int x,y;
    node(int a,int b)
    {
        x=a;
        y=b;
    }
    friend bool operator<(const node a,const node b)
    {
        if(a.x!=b.x)
            return a.x<b.x;
        return a.y<b.y;
    }

};
struct cmp
{
    bool operator()(node a,node b)
    {
        if(a.x!=b.x)
            return a.x<b.x;
        return a.y<b.y;
    }
};

int main()
{
    list<node> q;
    //node k;
    node a1(3,4);
    node a2(7,8);
    node a3(6,5);
    q.push_back(a1);
    q.push_back(a2);
    q.push_back(a3);
    //less<node> ptr;
    //q.sort(ptr);                OK
    //q.sort(less<node>() );      OK
    cmp k;
    q.sort(cmp());                //或者q.sort(k)
    for(list<node>::iterator i=q.begin(); i!=q.end(); i++)
        cout<<i->x<<' '<<i->y<<endl;
    return 0;
}