简介:

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

成员函数(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. CentOS7和win7双系统启动项

    1.添加win7启动项编辑文件 /boot/grub2/grub2.conf menuentry 'Windows 7' { insmod ntfs set root='hd0,1' chainloa ...

  2. linux系统中批量查找文件与文件内容的方法

    在linux中查看与修改文件权限我们都必须使用命令来操作,不能像windows一样点几下就好了,下面我们简单的介绍一下linux中的相关命令 比如查找当前目录下面所有的php文件里面某个关键字 fin ...

  3. package.json

    1,项目按住shift,右击鼠标:"在此处打开命令行窗口" 2,cmd输入:npm init 输入name,varsion....license项的信息,yes 3,此项目中自动创 ...

  4. python实现从生成器中取固定角标的元素

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- class A: def __init__(self): def r(): i = 1 while Tru ...

  5. Spring IoC容器初始化过程学习

    IoC容器是什么?IoC文英全称Inversion of Control,即控制反转,我么可以这么理解IoC容器: 把某些业务对象的的控制权交给一个平台或者框架来同一管理,这个同一管理的平台可以称为I ...

  6. Java线程:线程的同步-同步方法

    Java线程:线程的同步-同步方法   线程的同步是保证多线程安全访问竞争资源的一种手段. 线程的同步是Java多线程编程的难点,往往开发者搞不清楚什么是竞争资源.什么时候需要考虑同步,怎么同步等等问 ...

  7. EntityFramework优缺点

    高层视图: 改变在现有系统使用EntityFramework的优势是什么? • All -in-1框架的类映射表,需要编写映射代码, 并且是很难维护的. • 可维护性,易于理解的代码,无需创造大的数据 ...

  8. 使用RestTemplate发送post请求

    最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...

  9. EasyUI分页

    $("#tanModelBox1").css("display","none"); $('#dg').datagrid({ fitColum ...

  10. 导入excel2007文件问题

    基于oledb方式导入excel2007文件时,使用如下链接字符串: string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Sour ...