priority_queue实现

#include <algorithm>

using namespace std;
/*
priority_queue只允许在底端加入元素,并从顶端取出元素,
其内部元素不是依照被推入的次序排列,而是自动按照元素的权值排列,权值最大的元素排在最前面
缺省情况下priority_queue是利用一个max_heap完成
*/
template <class T,class Sequence=vector<T>,class Compare=less<typename Sequence::value_type>>
class priority_queue
{
public:
    typedef typename Sequence::value_type value_type;
    typedef typename Sequence::size_type size_type;
    typedef typename Sequence::reference reference;
    typedef typename Sequence::const_reference const_reference;
protected:
    Sequence c;//底层容器
    Compare comp;//元素大小比较标准
public:
    priority_queue() :c(){}
    explicit priority_queue(const Compare& x) :c(), comp(x){}

    //make_heap(),push_heap(),pop_heap()都是泛型算法
    template <class InputIterator>
    priority_queue(InputIterator first, InputIterator last, const Compare& x) : c(first, last), comp(x)
    {
        make_heap(c.begin(), c.end(), comp);
    }
    template <class InputIterator>
    priority_queue(InputIterator first, InputIterator last) : c(first,last)
    {
        make_heap(c.begin(), c.end(), comp);
    }

    void push(const value_type &x)
    {
        __STL_TRY{
        c.push_back(x);
        //重排heap
        push_heap(c.begin(), c.end(), comp);
    }
        __STL_UNWIND(c.clear());
    }

    void pop()
    {
        __STL_TRY{
        pop_heap(c.begin(), c.end(), comp);
        c.pop_back();
    }
        __STL_UNWIND(c.clear());
    }
};
时间: 2024-08-02 05:10:52

priority_queue实现的相关文章

priority_queue C++

三种优先队列定义方法:T_T 内部原理以后补..... priority_queue<int> qi;//普通的优先级队列,按从大到小排序 struct Node { friend bool operator< (Node a, Node b) { // 重载运算符自己定义优先级 return a.val > b.val; // 从小到大排 } int val; }; priority_queue<int, vector<int>, greater<int&

priority_queue使用

在c++的STL中priority_queue相当于堆,使用的操作有push(), pop(), top()等: priority_queue的头文件为<queue> 使用方法及实例: #include <iostream> #include <queue> #include<functional> using namespace std; struct node{ int idx; int key; node(int a=0, int b=0):idx(a

【STL】c++ priority_queue的使用方法

最开始在项目文档看到priority_queue这个模板时,还以为是自己定义的呢,后来查了一下,原来这是STL中存在的一种优先队列. 1.最简单的使用方法 std::priority_queue<int> q;默认从大到小 #include <iostream> #include <queue> #include <vector> int main() { std::priority_queue<int> q; for(int i=0;i<

STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)

向量(vector) <vector> 连续存储的元素<vector> Vector<int>c; c.back() 传回最后一个数据,不检查这个数据是否存在. c.clear() 移除容器中所有数据. c.empty() 判断容器是否为空. c.front() 传回地一个数据. c.pop_back() 删除最后一个数据. c.push_back(elem) 在尾部加入一个数据. c[i] 等同于 c.at(i); 列表(list) <list> 由节点组

hdu 4857 逆拓扑+大根堆(priority_queue)

题意:排序输出:在先满足定约束条件下(如 3必需在1前面,7必需在4前面),在满足:1尽量前,其次考虑2,依次.....(即有次约束). 开始的时候,只用拓扑,然后每次在都可以选的时候,优先考虑小的,其实没什么简单,如 图(3-->1,2)这样输出是2.3.1,正确应该是 3 1 2,因为 1要尽量前(都满足第一约束). 参考他人思路结合自己理解:因为这样的弊端就是没有考虑这种情况:图中:若我的"子孙"中,有的比你次优先,虽然现在我们都可以输出,但是要考虑我的子代,若我的子代有次

STL--容器适配器(queue、priority_queue、stack)

适配器(Adaptor)是提供接口映射的模板类.适配器基于其他类来实现新的功能,成员函数可以被添加.隐藏,也可合并以得到新的功能. STL提供了三个容器适配器:queue.priority_queue.stack. 这些适配器都是包装了vector.list.deque中某个顺序容器的包装器.注意:适配器没有提供迭代器,也不能同时插入或删除多个元素. 本文地址:http://www.cnblogs.com/archimedes/p/cpp-adapter.html,转载请注明源地址. 队列(qu

STL 之 queue、priority_queue 源码剖析

/* * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all c

priority_queue优先级队列总结

http://www.cppblog.com/Darren/archive/2009/06/09/87224.html priority_queue用法 priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法实现,也算是堆的另外一种形式. 先写一个用 STL 里面堆算法实现的与真正的STL里面的 priority_queue  用法相似的 priority_queue, 以加深对 priority_queue 的理解 #i

priority_queue 优先队列

优先队列是单向队列的一种,可以按照默认或自定义的一种方式来对队列中的数据进行动态排序 template<class _Ty, class _Container = vector<_Ty>, class _Pr = less<typename _Container::value_type> > //默认以vector为容器的 class priority_queue { // priority queue implemented with a _Container pub

浅谈C++ STL中的优先队列(priority_queue)

从我以前的博文能看出来,我是一个队列爱好者,很多并不是一定需要用队列实现的算法我也会采用队列实现,主要是由于队列和人的直觉思维的一致性导致的. 今天讲一讲优先队列(priority_queue),实际上,它的本质就是一个heap,我从STL中扒出了它的实现代码,大家可以参考一下. 首先函数在头文件<queue>中,归属于命名空间std,使用的时候需要注意. 队列有两种常用的声明方式: std::priority_queue<T> pq; std::priority_queue<