简介:

  优先队列是一种容器适配器,优先队列的第一个元素总是最大或最小的(自定义的数据类型需要重载运算符)。它是以堆为基础实现的一种数据结构。

成员函数(Member functions)

(constructor): Construct priority queue (public member function)
empty: Test whether container is empty (public member function)
size: Return size (public member function)
top: Access top element (public member function)
push: Insert element (public member function)
pop: Remove top element (public member function)

代码示例

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<functional>
#include<queue>
#include<vector> using namespace std;
int arr[] = {1, 9, 2, 8, 3, 7, 4, 5, 3, 5, 10, 8, 9}; struct node
{
friend bool operator < (node n1, node n2)
{
return n1.index < n2.index;
}
friend bool operator > (node n1, node n2)
{
return n1.index > n2.index;
} int index;
int value;
}; node b[]= {{10,100},
{99,50000},
{23,33},
{44,132},
{66,44}
}; int main()
{
//1.常见用法,默认最大元素优先
priority_queue<int> pq1;
for(int i = 0 ; i < sizeof(arr) / sizeof(int); i++)
pq1.push(arr[i]);
for(int i = 0 ; i < sizeof(arr) / sizeof(int); i++)
{
cout<<pq1.top()<<' ';
pq1.pop();
}
cout<<endl; //2.注意优先队列的申明方式
priority_queue<int,vector<int>, greater<int> > pq2;
for(int i = 0 ; i < sizeof(arr) / sizeof(int); i++)
pq2.push(arr[i]);
for(int i = 0 ; i < sizeof(arr) / sizeof(int); i++)
{
cout<<pq2.top()<<' ';
pq2.pop();
}
cout<<endl; //3.使用自定义的数据类型
priority_queue<node> pq3;
for(int i = 0; i < sizeof(b) / sizeof(node); i++)
pq3.push(b[i]);
for(int i = 0; i < sizeof(b) / sizeof(node); i++)
{
cout<<pq3.top().index<<' '<<pq3.top().value<<endl;
pq3.pop();
}
cout<<endl;
//4.
priority_queue<node, vector<node>, greater<node> > pq4;
for(int i = 0; i < sizeof(b) / sizeof(node); i++)
pq4.push(b[i]);
for(int i = 0; i < sizeof(b) / sizeof(node); i++)
{
cout<<pq4.top().index<<' '<<pq4.top().value<<endl;
pq4.pop();
} return 0;
}

  

STL-<queue>-priority queue的使用的更多相关文章

  1. [Algorithms] Queue & Priority Queue

    In this lesson, you will learn how to create a queue in JavaScript. A queue is a first-in, first-out ...

  2. [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)

    优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...

  3. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  4. stl源码分析之priority queue

    前面两篇介绍了gcc4.8的vector和list的源码实现,这是stl最常用了两种序列式容器.除了容器之外,stl还提供了一种借助容器实现特殊操作的组件,谓之适配器,比如stack,queue,pr ...

  5. find-median-from-data-stream & multiset priority queue 堆

    https://leetcode.com/problems/find-median-from-data-stream/ 这道题目实在是不错,所以单独拎出来. https://discuss.leetc ...

  6. [Algorithm] Heap & Priority queue

    这里只是简单的了解,具体内容详见推荐的原链接 注意堆和树的区别 堆就是优先级队列的实现形式 堆排序 排序过程 Ref: 排序算法之堆排序(Heapsort)解析 第一步(构造初始堆): {7, 5, ...

  7. 优先队列(Priority Queue)

    优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...

  8. Objective-C priority queue

    http://stackoverflow.com/questions/17684170/objective-c-priority-queue PriorityQueue.h // // Priorit ...

  9. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ

    命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...

随机推荐

  1. CSS will-change 属性

    介绍 如果你注意到在webkit的浏览器上“flicker”一些CSS操作(尤其是变形和动画方面的)的表现,你很可能之前就注意过硬件加速了 CPU.GPU和硬件加速 硬件加速意味着Graphics P ...

  2. MySQL-多条件拼接语句

    BEGIN "; SET @_where=""; THEN SET @_where= CONCAT(@_where," AND sourcedomain=\&q ...

  3. MFC的本质

    一.引言 上一专题中,纯手动地完成了一个Windows应用程序,然而,在实际开发中,我们大多数都是使用已有的类库来开发Windows应用程序.MFC(Microsoft Foundation Clas ...

  4. 全新的博客之旅&大学生活

    博客之旅: 刚刚申请了博客,感觉非常兴奋,整个人都变得有精神了. 想来几个月之前看到奇奇申了博客,在上面写文章,写各种解题报告,心里就好羡慕,好希望将来有一天,也能有一个属于自己的博客.由于之前课业压 ...

  5. iOS tableview删除多余的空cell

    self.tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 加一句这个,然后给tableview一个背景色, ...

  6. swift 2.x学习笔记(三)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0. ...

  7. EntityFramework Reverse POCO Code First 生成器

    功能强大的(免费)实体框架工具 Julie Lerman 实体框架是开源的,因此开发社区可以在 entityframework.codeplex.com 上共享代码. 但是不要将自己局限在那里寻找工具 ...

  8. DateUtil工具类

    package com.autoserve.mh.common.util;   import java.text.SimpleDateFormat; import java.util.Calendar ...

  9. Clustering Methods: Benefits and Limitations

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION

  10. How to use wget ?

    1.How to get conent (not download page) of website? wget <websit> -q -O -