STL-<queue>-priority queue的使用
简介:
优先队列是一种容器适配器,优先队列的第一个元素总是最大或最小的(自定义的数据类型需要重载运算符)。它是以堆为基础实现的一种数据结构。
成员函数(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的使用的更多相关文章
- [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 ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- STL之heap与优先级队列Priority Queue详解
一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...
- stl源码分析之priority queue
前面两篇介绍了gcc4.8的vector和list的源码实现,这是stl最常用了两种序列式容器.除了容器之外,stl还提供了一种借助容器实现特殊操作的组件,谓之适配器,比如stack,queue,pr ...
- find-median-from-data-stream & multiset priority queue 堆
https://leetcode.com/problems/find-median-from-data-stream/ 这道题目实在是不错,所以单独拎出来. https://discuss.leetc ...
- [Algorithm] Heap & Priority queue
这里只是简单的了解,具体内容详见推荐的原链接 注意堆和树的区别 堆就是优先级队列的实现形式 堆排序 排序过程 Ref: 排序算法之堆排序(Heapsort)解析 第一步(构造初始堆): {7, 5, ...
- 优先队列(Priority Queue)
优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...
- Objective-C priority queue
http://stackoverflow.com/questions/17684170/objective-c-priority-queue PriorityQueue.h // // Priorit ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ
命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...
随机推荐
- Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken
在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...
- dev checkbox多选
GridControl如果要支持多选,设置Options->OptionSeletion->MultiSelet为true就ok.
- SQL行合并
CREATE TABLE SC ( Student ), Course ) ) INSERT INTO SC SELECT N'张三',N'大学语文' UNION ALL SELECT N'李四',N ...
- MongoDB各种查询操作详解
这篇文章主要介绍了MongoDB各种查询操作详解,包括比较查询.关联查询.数组查询等,需要的朋友可以参考下 一.find操作 MongoDB中使用find来进行查询,通过指定find的第一个参数可 ...
- SQL模仿Split 功能
--方法0:动态SQL法 declare @s varchar(100),@sql varchar(1000) set @s='1,2,3,4,5,6,7,8,9,10' set @sql='sele ...
- Get请求中文乱码的几种解决方式
1.将字符串转码:new String("xxxxx".getBytes("iso-8859-1"),"utf-8") 这种 ...
- C# 根据年月日获取星期几方法
#region 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);) /// <summary> /// 根据年月日计算星期几(Label ...
- 【javaweb学习】解析XML
XML解析方式有两种 dom:Document Object Model文档对象模型,是w3c组织推荐的解析方式 sax:Simple Api XML不是官方标准,但它是XML社区实际上的标准,几乎所 ...
- django--models操作
1.models的功能 操作数据库 提交验证 在django的admin中,使用的是modelForms所以在验证的时候,尽管在models后有error_ message参数也不会根据此来提示.具体 ...
- java类初始化的过程
在复习Thinking in java的过程中看到了相关内容,顺便整理一下,像下面一样的代码具体的执行顺序(ABCD都是类) public class A { public A(String text ...