1、默认为大顶堆

 #include <iostream>
#include <queue>
using namespace std; int main()
{
priority_queue<int> p;
int i;
for (i = ; i < ; i++)
p.push(i);
for (i = ; i < ; i++)
{
cout << p.top() << " ";
p.pop();
}
return ;
}

运行结果:

2、小顶堆

 int main()
{
priority_queue<int, vector<int>, greater<int>> p;
//vs2013需要#include <functional>
int i;
for (i = ; i < ; i++)
p.push(i);
for (i = ; i < ; i++)
{
cout << p.top() << " ";
p.pop();
}
return ;
}

运行结果:

3、自定义

 #include <iostream>
#include <queue>
using namespace std; struct Node
{
int a, b;
Node(int x, int y) : a(x), b(y) {}
}; struct cmp
{
bool operator() (Node *x, Node *y)
{
if (x->a != y->a) return x->a < y->a; //大顶堆
return x->b < y->b;
}
};
int main()
{ priority_queue<Node *, vector<Node *>, cmp> p;
Node *n = new Node(, ); p.push(n);
n = new Node(, ); p.push(n);
n = new Node(, ); p.push(n);
while (!p.empty())
{
n = p.top();
p.pop();
cout << n->a << " " << n->b << endl;
}
return ;
}

运行结果:

c++ priority_queue的更多相关文章

  1. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  2. 【转载】STL之priority_queue

    参考资料:传送门先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部.priority_queue特别之处在于,允许用户为队列中存储的元素 ...

  3. STL之priority_queue

    下面以 long long 型队列介绍: Q.empty() // 判断队列是否为空 返回ture表示空 返回false表示空 bool Q.top() // 返回顶端元素的值 元素还在队列里 lon ...

  4. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  5. STL之容器适配器priority_queue

    priority_queue(优先队列)是一个拥有权值观念的queue,它允许加入新元素,删除旧元素,审视元素值等功能.由于这是一个queue,所以只允许在底端加入元素,并从顶端取出元素, 除此之外别 ...

  6. priority_queue 示例

    http://www.cplusplus.com/reference/queue/priority_queue/ priority_queue 的top始终保持着为一堆数据中的最大元素. 读取最小 O ...

  7. 优先队列priority_queue的比较函数

    STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...

  8. 5.1 stack,queue以及priority_queue

    *:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...

  9. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  10. priority_queue 优先队列用法

    //采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...

随机推荐

  1. mariaDB 远程连接不上

    GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; // %:表示从任何主机连接 ...

  2. Jupyter 环境配置

    1. 找到python文件目录, 用管理员身份打开powershell python -m pip install jupyter 2. Jupyter notebook

  3. 记录CentOS7.X版本下安装MySQL5.7数据库

    记录CentOS7.X版本下安装MySQL5.7数据库  设置rpm下载目录在/opt目录下新建一个目录存放mysql cd /opt sudo mkdir mysql12  下载MySQL的源 wg ...

  4. appium手机自动化环境搭建

    在robotframework环境安装完成的基础上进行如下安装,如果没有安装rfs环境,请先参考robotframework安装文章:Robot Framework的环境搭建 文件下载地址:链接:ht ...

  5. MyCnblog Style

    以下内容添加到页脚HTML代码处 <style> #leftmenu ul { display: none; } .cnblogs-markdown pre code, .cnblogs- ...

  6. 5W1H

    WHY   ——为什么做?原因与目标 WHAT ——做什么?目标与内容 WHO   ——谁去做?具体的执行者 WHEN ——什么时间做?执行时间 WHERE——在什么地方做?执行地点 HOW——怎样做 ...

  7. input 单选按钮radio 取消选中

    //需要先引入JQ.js <input name="rdo" value="AA" type="radio" tag="0& ...

  8. Unity ContextMenu 上下文菜单

    新建脚本: public class ContextTesting : MonoBehaviour { [ContextMenu("哈哈")] void DoSomething() ...

  9. (转)DB2 8.2 for aix5L安装和配置步骤

    DB2 8.2 for aix5L安装和配置步骤[@more@] AIX5.2上安装DB2 V8.2安装平台:IBM eserver,AIX 5.3 64位 一 删除以有的DB2系统: 1.停止DB2 ...

  10. git 摘要

    git使用摘记 git冲突的问题主要是在修改的部分而不是添加的部分, 如果merge的文件在同一个位置有不同的信息则git会报错 git push origin中的origin表示的是远程的仓库名为o ...