C++ STL(5):Priority Queue

1. Max Heap

Priority Queue 是 C++ 自带的一个容器,同时具有优先顺序,默认为最大元素在队首。

// TSWorld
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <algorithm>

using namespace std;

void Print(priority_queue<int>Max_heap)
{
    priority_queue<int>temp = Max_heap;

    while(!temp.empty()) {
        cout<<temp.top()<<" ";
        temp.pop();
    }

    cout<<endl;
}
int main()
{
    priority_queue<int>Max_heap;
    Max_heap.push(1);
    Max_heap.push(3);
    Max_heap.push(1);
    Max_heap.push(4);
    Max_heap.push(520);

    printf("The priority queue Max_heap is : ");
    Print(Max_heap);

    printf("The Max_heap.size() is : ");
    cout<<Max_heap.size()<<endl;

    printf("The Max_heap.top() is : ");
    cout<<Max_heap.top()<<endl;

    return 0;
}

Output:

The priority queue Max_heap is : 520 4 3 1 1
The Max_heap.size() is : 5
The Max_heap.top() is : 520

2. Min Heap

// TSWorld
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <algorithm>

using namespace std;

void Print(priority_queue<int, vector<int>, greater<int>>Min_heap)
{
    priority_queue<int, vector<int>, greater<int>>temp = Min_heap;

    while(!temp.empty()) {
        cout<<temp.top()<<" ";
        temp.pop();
    }

    cout<<endl;
}
int main()
{
    priority_queue<int, vector<int>, greater<int>>Min_heap;
    Min_heap.push(1);
    Min_heap.push(3);
    Min_heap.push(1);
    Min_heap.push(4);
    Min_heap.push(520);

    printf("The priority queue Min_heap is : ");
    Print(Min_heap);

    printf("The Min_heap.size() is : ");
    cout<<Min_heap.size()<<endl;

    printf("The Min_heap.top() is : ");
    cout<<Min_heap.top()<<endl;

    return 0;
}

Output:

The priority queue Min_heap is : 1 1 3 4 520 
The Min_heap.size() is : 5
The Min_heap.top() is : 1

3. 自定义排序

// TSWorld
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <algorithm>

using namespace std;

struct Person {
    string name;
    int score;
    Person(string n,int s):name(n),score(s){}
};

struct cmp {
    bool operator () (struct Person a,struct Person b) {
        if(a.score == b.score)
            return a.name > b.name;
        else
            return a.score < b.score;
    }
};

void Print(priority_queue<Person,vector<Person>,cmp>Data)
{
    priority_queue<Person,vector<Person>,cmp>temp = Data;

    while(!temp.empty()) {
        cout<<temp.top().name<<" "<<temp.top().score<<endl;
        temp.pop();
    }
}
int main()
{

    priority_queue<Person,vector<Person>,cmp>Data;

    Data.push(Person("TSWorld",99));
    Data.push(Person("Hao",66));
    Data.push(Person("He",66));
    Data.push(Person("J++",60));

    printf("The priority queue Data is : \n");
    Print(Data);

    printf("The Data.size() is : ");
    cout<<Data.size()<<endl;

    printf("The Data.top() is : ");
    cout<<Data.top().name<<" "<<Data.top().score<<endl;
    return 0;
}

Output:

The priority queue Data is :
TSWorld 99
Hao 66
He 66
J++ 60
The Data.size() is : 4
The Data.top() is : TSWorld 99
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值