C++ STL 学习笔记__(6)优先级队列priority_queue基本操作

10.2.7优先级队列priority_queue

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

v  优先级队列适配器 STL priority_queue

v  用来开发一些特殊的应用,请对stl的类库,多做扩展性学习

         priority_queue<int, deque<int>>   pq;

         priority_queue<int, vector<int>>   pq;

         pq.empty()

         pq.size()

         pq.top()

         pq.pop()

         pq.push(item)

#include <iostream>

using namespace std;

#include "queue"

void main81()

{

         priority_queue<int> p1; //默认是 最大值优先级队列

         //priority_queue<int, vector<int>, less<int> > p1; //相当于这样写

         priority_queue<int, vector<int>, greater<int>> p2; //最小值优先级队列

 

         p1.push(33);

         p1.push(11);

         p1.push(55);

         p1.push(22);

         cout <<"队列大小" << p1.size() << endl;

         cout <<"队头" << 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();

 

         }

}

#include<iostream>
using namespace std;
#include "queue"

void main81()
{
    priority_queue<int>p1;//默认情况下,最大值优先级队列
    priority_queue<int,vector<int>,less<int>> p2; //提前定义好的预定义函数 谓词
    priority_queue<int,vector<int>,greater<int>>p3;//最小值优先队列

    p1.push(33);
    p1.push(11);
    p1.push(55);
    p1.push(22);

    cout<<"队列元素"<<p1.top()<<endl;
    cout<<"队列的大小"<<p1.size()<<endl;

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

    cout<<endl<<"最小值优先级队列"<<endl;
    p3.push(33);
    p3.push(11);
    p3.push(55);
    p3.push(22);

    cout<<"队列元素"<<p3.top()<<endl;
    cout<<"队列的大小"<<p3.size()<<endl;

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

}
void main()
{
    main81();
    cout<<"hello..."<<endl;
    return;
}

 

资料来源:传智播客

posted on 2017-01-11 09:16  金秀  阅读(214)  评论(0编辑  收藏  举报

导航