码迷,mamicode.com
首页 > 其他好文 > 详细

STL:优先队列Priority Aueue

时间:2018-03-16 11:19:55      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:first   cti   syn   alt   iostream   stream   mes   let   name   

The functions associated with priority queue are:
empty() – Returns whether the queue is empty
size() – Returns the size of the queue
top() – Returns a reference to the top most element of the queue
push(g) – Adds the element ‘g’ at the end of the queue
pop() – Deletes the first element of the queue

#include <iostream>
#include <queue>
 
using namespace std;
 
void showpq(priority_queue <int> gq)
{
    priority_queue <int> g = gq;
    while (!g.empty())
    {
        cout << ‘\t‘ << g.top();
        g.pop();
    }
    cout << ‘\n‘;
}
 
int main ()
{
    priority_queue <int> gquiz;
    gquiz.push(10);
    gquiz.push(30);
    gquiz.push(20);
    gquiz.push(5);
    gquiz.push(1);
 
    cout << "The priority queue gquiz is : ";
    showpq(gquiz);
 
    cout << "\ngquiz.size() : " << gquiz.size();
    cout << "\ngquiz.top() : " << gquiz.top();
 
 
    cout << "\ngquiz.pop() : ";
    gquiz.pop();
    showpq(gquiz);
 
    return 0;
}

 
The output of the above programs is :

The priority queue gquiz is :     30    20    10    5    1

gquiz.size() : 5
gquiz.top() : 30
gquiz.pop() :     20    10    5    1

STL:优先队列Priority Aueue

标签:first   cti   syn   alt   iostream   stream   mes   let   name   

原文地址:https://www.cnblogs.com/passion-sky/p/8579363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!