关于结构体

关于 初始化。赋值。排序。

优先队列的问题; 

详细的去 wxj博客看吧。https://blog.csdn.net/qq_40482358/article/details/81239448

结构体初始 重载。 

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int a,b;
    node(int _a=0,int _b=0):a(_a),b(_b){}
    //结构体的直接赋值
    /*node(int _a=0,int _b=0)
    {
        a=_a;
        b=_b;
    }
    结构体的初始化赋值,a,b初始化为 0
    */

    /*
    bool operator < (const node &x)const{
        return x.a<a;
    }
     重载 < 运算符 。 结构体中 a 元素 小的结构体 优先
    */

    /*
    bool operator <(const node &x)const{
        if(x.a==a) return x.b<b;
        return x.a<a;
    }
    a元素相等时,b元素小的结构体优先
    */
};
vector <node> n;
priority_queue<int > que;
//优先队列默认排序 max->min
int main()
{
    n.push_back(node(1,2));//直接将a=1,b=2放入结构体中
    cout<<n[0].a<<" "<<n[0].b<<endl;
    /*
    output : 1 2 (wxj大佬的习惯很好,要多多向他学习)
    */
    que.push(1);
    que.push(12);
    que.push(123213);
    while(que.size())
    {
        cout<<que.top()<<" ";//每次取出首元素
        que.pop();
    }
    /*
    output : 123213 12 1
    */
    return 0;
}

优先队列 less<> 从大到小排列 

#include <bits/stdc++.h>
using namespace std;
/*
less< >,greater< >
是系统自带的一些库函数
*/
priority_queue<int,vector<int>,less<int> >que;//大->小
int main()
{
    que.push(1);
    que.push(2);
    que.push(3);
    while(que.size())
    {
        cout<<que.top()<<" ";
        que.pop();
    }
    /*
    output : 3 2 1
    */
    return 0;
}

优先队列 greater<> 从小到大 排列 

#include <bits/stdc++.h>
using namespace std;
/*
less< >,greater< >
是系统自带的一些库函数
*/
priority_queue<int,vector<int>,greater<int> >que;//小->大
int main()
{
    que.push(1);
    que.push(2);
    que.push(3);
    while(que.size())
    {
        cout<<que.top()<<" ";
        que.pop();
    }
    /*
    output : 1 2 3
    */
    return 0;
}

 优先队列 自定义排序

#include <bits/stdc++.h>
using namespace std;
struct node{
    int a,b;
    node(int _a=0,int _b=0):a(_a),b(_b){}
};
struct cmp{
    //a 大的 优先
    bool operator()(const node &x,const node &y)
    {
        return x.a<y.a;
    }
};
priority_queue<node,vector<node>,cmp> que;
int main()
{
    que.push(node(1,2));
    que.push(node(123,12));
    que.push(node(89,3));
    while(que.size())
    {
        cout<<que.top().a<<" "<<que.top().b<<endl;
        que.pop();
    }
    /*
    123 12
    89 3
    1 2

    */
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
struct node{
    int a,b;
    node(int _a=0,int _b=0):a(_a),b(_b){}
};
struct cmp{
    //a 元素相同 b元素大的优先
    bool operator()(const node &x,const node &y)
    {
        if(x.a==y.a)
            return x.b<y.b;
        return x.a<y.a;
    }
};
priority_queue<node,vector<node>,cmp> que;
int main()
{
    que.push(node(1,2));
    que.push(node(123,12));
    que.push(node(89,3));
    que.push(node(123,156));
    while(que.size())
    {
        cout<<que.top().a<<" "<<que.top().b<<endl;
        que.pop();
    }
    /*
    123 156
    123 12
    89 3
    1 2
    */
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值