【STL】模拟实现优先级队列

优先级队列(priority_queue)介绍

  1. 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。
  2. 在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素。
  3. 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。
  4. 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:
    empty():检测容器是否为空
    size():返回容器中有效元素个数
    front():返回容器中第一个元素的引用
    push_back():在容器尾部插入元素
    pop_back():删除容器尾部元素
  5. 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。
  6. 需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。
    优先级队列默认使用vector作为其底层存储数据的容器,在vector上又使用了堆算法将vector中元素构造成堆的结构,因此priority_queue就是堆,所有需要用到堆的位置,都可以考虑使用priority_queue。注意:默认情况下priority_queue是大堆。

仿函数

仿函数本质就是类重载了一个operator(),创建一个行为类似函数的对象,我们可以通过这个对象调整优先级队列是大堆还是小堆,调用STL中的仿函数需要包头文件#inclide< functional>,仿函数比函数指针更加灵活。

在这里插入图片描述

 template<class T>
    struct less {
        bool operator()(const T& left, const T& right){
            return left < right;
        }
    };
    template<class T>
    struct greater {
        bool operator()(const T& left, const T& right) {
            return left > right;
        }
    };
 

int main()
{
//Less->仿函数类型 les->仿函数对象
Less les;
les.operator()(2, 5)
cout << ls(2. 5) << endl; // 2 < 5, 输出1
cout << Greater()(2, 5) << endl;//匿名对象, 输出0
}

这里less是建大堆,greater是建小堆,可以根据仿函数Compare是less还是greater,调整向上和向下调整算法,进而实现大小堆的切换。

模拟实现

#pragma once
#include<iostream>
#include<queue>;
#include<algorithm>
#include<assert.h>
using namespace std;
namespace lc {
    template<class T>
    struct less {
        bool operator()(const T& left, const T& right){
            return left < right;
        }
    };
    template<class T>
    struct greater {
        bool operator()(const T& left, const T& right) {
            return left > right;
        }
    };
	template<class T, class Container = vector<int>, class com = less<T>>
	class priority_queue {
    public:

        priority_queue()
            :c()
        {}
        void adjustDown(T& parent) {
            int child = parent * 2 + 1;
            while (child < c.size()) {
                if (child + 1 < c.size() && com()(c[child], c[child + 1])) child++;
                if (com()(c[parent], c[child])) {
                    swap(c[parent], c[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else break;
            }
        }
        void adjustUp(T& child) {
            int parent = (child - 1) >> 1;
            while (child) {
                if (com()(c[child], c[parent])) break;
                else {
                    swap(c[child], c[parent]);
                    child = parent;
                    parent = (child - 1) >> 1;
                }
            }
        }
        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
            : c(first, last)
        {
            int cot = c.size();
            int parent = (cot - 2) >> 1;
            while (parent >= 0) {
                adjustDown(parent);
                parent--;
            }
        }

        bool empty() const {
            return c.empty();
        }

        size_t size() const {
            return c.size();
        }

        T& top() const {
            return c.front();
        }

        void push(const T& x) {
            c.push_back(x);
            adjustUp(c.size()-1);
        }

        void pop() {
            assert(c.empty());
            swap(c.front(), c.back());
            c.pop_back();
            adjustDown(0);
        }

    private:
        Container c;
	};
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁华的梦境

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值