优先级队列相对于普通队列,提供了插队功能,每次最先出队的不是最先入队的元素,而是优先级最高的元素。

它的实现采用了标准库提供的heap算法。该系列算法一共提供了四个函数。使用方式如下:

首先,建立一个容器,放入元素:

vector<int> coll;
insertNums(coll, 3, 7);
insertNums(coll, 5, 9);
insertNums(coll, 1, 4); printElems(coll, "all elements: ");

打印结果为:

all elements:
3 4 5 6 7 5 6 7 8 9 1 2 3 4

然后我们调用make_heap,这个算法把[beg, end)内的元素建立成堆

make_heap(coll.begin(), coll.end());

printElems(coll, "after make_heap: ");

打印结果:

after make_heap:
9 8 6 7 7 5 5 3 6 4 1 2 3 4

然后我们调用pop_heap,这个算法必须保证[beg, end)已经是一个heap,然后它将堆顶的元素(其实是begin指向的元素)放到最后,再把[begin. end-1)内的元素重新调整为heap

pop_heap(coll.begin(), coll.end());
coll.pop_back();
printElems(coll, "after pop_heap: ");

打印结果为:

after pop_heap:
8 7 6 7 4 5 5 3 6 4 1 2 3

接下来我们调用push_heap,该算法必须保证[beg, end-1)已经是一个heap,然后将整个[beg, end)调整为heap

coll.push_back(17);
push_heap(coll.begin(), coll.end()); printElems(coll, "after push_heap: ");

打印结果为:

after push_heap:
17 7 8 7 4 5 6 3 6 4 1 2 3 5

最后我们使用sort_heap将[beg, end)由heap转化为有序序列,所以,前提是[beg, end)已经是一个heap

sort_heap(coll.begin(), coll.end());
printElems(coll, "after sort_heap: ");

打印结果为:

after sort_heap:
1 2 3 3 4 4 5 5 6 6 7 7 8 17

完整的测试代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; template <typename T>
void insertNums(T &t, int beg, int end)
{
while(beg <= end)
{
t.insert(t.end(), beg);
++beg;
}
} template <typename T>
void printElems(const T &t, const string &s = "")
{
cout << s << endl;
for(typename T::const_iterator it = t.begin();
it != t.end();
++it)
{
cout << *it << " ";
}
cout << endl;
} int main(int argc, char const *argv[])
{
vector<int> coll;
insertNums(coll, 3, 7);
insertNums(coll, 5, 9);
insertNums(coll, 1, 4); printElems(coll, "all elements: "); //在这个范围内构造heap
make_heap(coll.begin(), coll.end()); printElems(coll, "after make_heap: "); //将堆首放到最后一个位置,其余位置调整成堆
pop_heap(coll.begin(), coll.end());
coll.pop_back();
printElems(coll, "after pop_heap: "); coll.push_back(17);
push_heap(coll.begin(), coll.end()); printElems(coll, "after push_heap: "); sort_heap(coll.begin(), coll.end());
printElems(coll, "after sort_heap: "); return 0;
}

 

根据以上的算法,我们来实现标准库的优先级队列priority_queue,代码如下:

#ifndef PRIORITY_QUEUE_HPP
#define PRIORITY_QUEUE_HPP #include <vector>
#include <algorithm>
#include <functional> template <typename T,
typename Container = std::vector<T>,
typename Compare = std::less<typename Container::value_type> >
class PriorityQueue
{
public:
typedef typename Container::value_type value_type; //不用T
typedef typename Container::size_type size_type;
typedef Container container_type;
typedef value_type &reference;
typedef const value_type &const_reference; PriorityQueue(const Compare& comp = Compare(),
const Container& ctnr = Container());
template <class InputIterator>
PriorityQueue (InputIterator first, InputIterator last,
const Compare& comp = Compare(),
const Container& ctnr = Container());
void push(const value_type &val)
{
cont_.push_back(val);
//调整最后一个元素入堆
std::push_heap(cont_.begin(), cont_.end(), comp_);
} void pop()
{
//第一个元素移出堆,放在最后
std::pop_heap(cont_.begin(), cont_.end(), comp_);
cont_.pop_back();
} bool empty() const { return cont_.empty(); }
size_type size() const { return cont_.size(); }
const_reference top() const { return cont_.front(); } private:
Compare comp_; //比较规则
Container cont_; //内部容器
}; template <typename T, typename Container, typename Compare>
PriorityQueue<T, Container, Compare>::PriorityQueue(const Compare& comp,
const Container& ctnr)
:comp_(comp), cont_(ctnr)
{
std::make_heap(cont_.begin(), cont_.end(), comp_); //建堆
} template <typename T, typename Container, typename Compare>
template <class InputIterator>
PriorityQueue<T, Container, Compare>::PriorityQueue (InputIterator first,
InputIterator last,
const Compare& comp,
const Container& ctnr)
:comp_(comp), cont_(ctnr)
{
cont_.insert(cont_.end(), first, last);
std::make_heap(cont_.begin(), cont_.end(), comp_);
} #endif //PRIORITY_QUEUE_HPP

我们注意到:

1.优先级队列内部保存了排序规则,这与map和set是一致的。

2.前面我们提到heap算法除了make_heap之外,都必须保证之前是一个建好的heap,这里我们在构造函数中调用make_heap,保证了后面的各种heap算法都是合法的。

3.还有一点,如果T与容器的类型不一致,例如PriorityQueue<float, vector<int> >,那么我们的value_type优先采用int,毕竟我们操作的对象是容器。

测试代码如下:

#include "PriorityQueue.hpp"
#include <iostream>
using namespace std; int main(int argc, char const *argv[])
{
PriorityQueue<float> q;
q.push(66.6);
q.push(22.3);
q.push(44.4); cout << q.top() << endl;
q.pop();
cout << q.top() << endl;
q.pop(); q.push(11.1);
q.push(55.5);
q.push(33.3);
q.pop(); while(!q.empty())
{
cout << q.top() << " ";
q.pop();
}
cout << endl; return 0;
}

标准库priority_queue的一种实现的更多相关文章

  1. 标准库Stack的一种实现

    本文实现了STL中stack的大部分功能,同时添加了一些功能. 注意以下几点: 1.Stack是一种适配器,底层以vector.list.deque等实现 2.Stack不含有迭代器 在本例中,我添加 ...

  2. C++ 异常机制分析(C++标准库定义了12种异常,很多大公司的C++编码规范也是明确禁止使用异常的,如google、Qt)

    阅读目录 C++异常机制概述 throw 关键字 异常对象 catch 关键字 栈展开.RAII 异常机制与构造函数 异常机制与析构函数 noexcept修饰符与noexcept操作符 异常处理的性能 ...

  3. C++ 标准库类型-String,Vector and Bitset

    <C++ Primer 4th>读书摘要 最重要的标准库类型是 string 和 vector,它们分别定义了大小可变的字符串和集合.这些标准库类型是语言组成部分中更基本的那些数据类型(如 ...

  4. 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd

    http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少,后来发现这两个函数 ...

  5. Python内置模块与标准库

    Python内置模块就是标准库(模块)吗?或者说Python的自带string模块是内置模块吗? 答案是:string不是内置模块,它是标准库.也就是说Python内置模块和标准库并不是同一种东西. ...

  6. Python标准库:1. 介绍

    标准库包括了几种不同类型的库. 首先是那些核心语言的数据类型库,比方数字和列表相关的库.在核心语言手冊里仅仅是描写叙述数字和列表的编写方式,以及它的排列,而未定义它的语义. 换一句话说,核心语言手冊仅 ...

  7. C++标准库(体系结构与内核分析)(侯捷第二讲)

    一.OOP和GP的区别(video7) OOP:面向对象编程(Object-Oriented programming) GP:泛化编程(Generic programming) 对于OOP来说,我们要 ...

  8. 谈谈两种标准库类型---string和vector

    两种最重要的标准库---string和vector string和vector是两种最重要的标准库类型,string表示可变长的字符序列,vector存放的是某种给定类型对象的可变长序列. 一.标准库 ...

  9. 140种Python标准库、第三方库和外部工具

    导读:Python数据工具箱涵盖从数据源到数据可视化的完整流程中涉及到的常用库.函数和外部工具.其中既有Python内置函数和标准库,又有第三方库和工具. 这些库可用于文件读写.网络抓取和解析.数据连 ...

随机推荐

  1. Linux装软件

    一.rpm包安装方式步骤: 1.找到相应的软件包,比如soft.version.rpm,下载到本机某个目录: 2.打开一个终端,su -成root用户: 3.cd soft.version.rpm所在 ...

  2. 汕头市队赛 SRM 08 A

    比赛没参加 所以回来补题咯 A还是自己YY出来了的 可惜比赛没有打 描述 给一个 01 串设为其 S,询问是否存在只出现两次的 01 串 T. 这里的出现定义为存在一串下标 ,满足  且 . 输入格式 ...

  3. JavaEE中Filter实现用户登录拦截

    实现思路是编写过滤器,如果用户登录之后session中会存一个user.如果未登录就为null,就可以通过过滤器将用户重定向到登陆页面,让用户进行登陆,当然过滤器得判断用户访问的如果是登陆请求需要放行 ...

  4. Git 的使用教程

    Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理. 安装Git 1.方法一:安装homebrew,然后通过homebrew安装Git. 第一步:在终端输入如下命令 ...

  5. Java遍历list集合的4种方法

    list集合的遍历4种方法: package com.sort; import java.util.ArrayList; import java.util.Iterator; import java. ...

  6. 漂亮的弹窗口插件——sweetAlert的使用

    想必你已经受够了单调的alert弹窗吧? 为了更好的用户体验性,现在介绍一款漂亮的弹窗口插件——sweetAlert,现在就来介绍它的使用 1.首先在官网下载它的CSS和JavaScript文件:ht ...

  7. [BZOJ1854][SCOI2010]游戏 二分图最大匹

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 5316  Solved: 2128[Submit][Status] ...

  8. JavaScript 性能优化的小知识总结

    前言 一直在学习 javascript,也有看过<犀利开发 Jquery 内核详解与实践>,对这本书的评价只有两个字犀利,可能是对 javascript 理解的还不够透彻异或是自己太笨,更 ...

  9. 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)

    M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...

  10. Codeforces 798 B. Mike and strings-String的find()函数

    好久好久好久之前的一个题,今天翻cf,发现这个题没过,补一下. B. Mike and strings time limit per test 2 seconds memory limit per t ...