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

它的实现采用了标准库提供的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. [ CodeVS冲杯之路 ] P1295

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/1295/ 数据很小,直接DFS,加上剪枝 剪枝其实就是判重,首先深度是行下标,这里自带不重复,枚举的列下标,用 f 记录 ...

  2. mlock家族:锁定物理内存【转】

    转自:http://blog.csdn.net/fjt19900921/article/details/8074541 锁住内存是为了防止这段内存被操作系统swap掉.并且由于此操作风险高,仅超级用户 ...

  3. 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---11

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  4. 华为上机测试题(Excel表格纵列字母数字转换-java)

    PS:这是我刚做的一道题,题目不难,满分60,得分40,大家看看哪里有问题,欢迎提意见,感谢! /* * 题目:Excel表格纵列字母数字转换 * 描述: 在Excel中列的编号为A-Z,AA-AZ, ...

  5. 非MFC工程中使用MFC库

    目录(?)[-] 需求说明 常见问题 问题分析 参考解决方法 我的解决方案 Stdafxh的原理   需求说明 C++工程的类型有很多,从VS(或VC)可以看到常见的有:Win32 Console A ...

  6. python每日一类(4):slice

    class slice(stop)class slice(start, stop[, step]) Return a slice object representing the set of indi ...

  7. 什么是web前端,全栈工程师就业前景怎么样?

    Web全栈工程师 什么是web前端? Web为你在浏览器.APP.应用程序等设备上提供直观界面,这些界面展现以及用户交互就是前端. 从2016年到2017年,web前端岗位从之前的爆发式增长变为平稳的 ...

  8. (4)C#工具箱-菜单和工具栏

    1.ContextMenuStrip(右键菜单栏) 把contextMenuStrip控件拖到窗体上,会在窗体下面显示,点击控件在最上行显示菜单栏,可以任意设置.(运行以后不会在界面上显示,它需要预控 ...

  9. 日志采集客户端 filebeat 安装部署

    linux----------------1. 下载wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.5.1- ...

  10. ReactiveCocoa(二)

    前言 通过ReactiveCocoa(一)的学习,相信大家对ReactiveCocoa有了一些基本认识吧.下面就让我们来学习ReactiveCocoa的一些基本使用吧! ReactiveCocoa基本 ...