优先级队列priority_queue

一、优先级队列priority_queue

  最大值优先级队列、最小值优先级队列

#include "pch.h"
#include <iostream>
#include"queue"
using namespace std;

int main()
{    
    priority_queue<int> p1;//默认从大到小
    priority_queue<int, vector<int>, greater<int>> p2;//设定为从小到大
    p1.push(33);
    p1.push(11);
    p1.push(55);
    p1.push(22);

    cout << "p1队列大小:" << p1.size() << endl;
    cout << "p1对头:" << p1.top() << endl;

    while (p1.size() > 0)
    {
        cout << p1.top() << " ";
        p1.pop();
    }
    cout << endl;

    cout << "测试最小值优先级队列" << endl;
    p2.push(33);
    p2.push(11);
    p2.push(55);
    p2.push(22);
    while (p2.size() > 0)
    {
        cout << p2.top() << " ";
        p2.pop();

    }
    cout << endl;

    std::cout << "Hello World!\n"; 
}

 

posted @ 2019-03-30 10:22  星星之火可以燎源  阅读(131)  评论(0编辑  收藏  举报