priority_queue::push();pop(),empty,back(),front();size()

       下面的代码示例阐释了如何使用 Visual c + + 中的 priority_queue::push、 priority_queue::pop、 priority_queue::empty、 priority_queue::top,和 priority_queue::size STL 函数。

priority_queue 适配器包含支持该 priority_queue 的容器的类型定义类型的对象。支持的两个容器都是向量并在 deque。对象插入的 push (),并且 pop() 被删除。top() 返回该 priority_queue 顶部的项目。

由于适配器不支持迭代,一个 priority_queue 有没有相关联的迭代器。

Priority_queue 允许您维护的项由一个相关联的比较器功能的比如小于,大于,等的已排序的集合。因此,顶部的项目将成为的最小或最高基于所选函数的选择候选。

priority_queue::push();

 priority_queue::pop();

priority_queue::empty();

priority_queue::top();

priority_queue::size();

 

// Compile options needed: /GX
// 
// <filename> :  priority_queue.cpp
// 
// Functions:
// 
//    priority_queue::push(), priority_queue::pop(),
//    priority_queue::empty(), priority_queue::top(), queue::size()
// 
// Written by Debabrata Sarma
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
// 

#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <functional>
using namespace std;

#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
   #endif

// Using priority_queue with deque
// Use of function greater sorts the items in ascending order
typedef deque<int, allocator<int> > INTDQU;
typedef priority_queue<int,INTDQU, greater<int> > INTPRQUE;

// Using priority_queue with vector
// Use of function less sorts the items in descending order
typedef vector<char, allocator<char> > CHVECTOR;
typedef priority_queue<char,CHVECTOR,less<char> > CHPRQUE;

void main(void)
{
    int size_q;
    INTPRQUE   q;
    CHPRQUE    p;

    // Insert items in the priority_queue(uses deque)
    q.push(42);
    q.push(100);
    q.push(49);
    q.push(201);

    // Output the item at the top using top()
    cout << q.top() << endl;
    // Output the size of priority_queue
    size_q = q.size();
    cout << "size of q is:" << size_q << endl;
    // Output items in priority_queue using top()
    // and use pop() to get to next item until
    // priority_queue is empty
    while (!q.empty())
    {
        cout << q.top() << endl;
        q.pop();
    }

// Insert items in the priority_queue(uses vector)
    p.push('c');
    p.push('a');
    p.push('d');
    p.push('m');
    p.push('h');

    // Output the item at the top using top()
    cout << p.top() << endl;

    // Output the size of priority_queue
    size_q = p.size();
    cout << "size of p is:" << size_q << endl;

    // Output items in priority_queue using top()
    // and use pop() to get to next item until
    // priority_queue is empty
    while (!p.empty())
    {
        cout << p.top() << endl;
        p.pop();
    }
}程序输出:

42
q 的大小是: 4
42
49
100
201
m
p 的大小是: 5
m
h
d
c


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值