当前位置: 首页 > java >正文

C++优先级队列priority_queue的模拟实现

1.优先级队列的模拟实现

优先级队列默认使用vector作为其底层存储数据的容器,在vector上又使用了堆算法将vector中元素构造成堆的结构,因此priority_queue就是堆。

#pragma once
#include<vector>
#include<iostream>
#include<functional>
#include <algorithm>
using namespace std;
namespace my
{
template<class T, class Container = vector<T>, class Compare = less<T>>
class priority_queue
{
public:
priority_queue()
{}

        template<class InputIterator>
priority_queue(InputIterator first, InputIterator last)
{
while (first != last)
{
_con.push_back(*first);
++first;
}
//向下调整建堆
for (int i = (_con.size() - 2) / 2; i >= 0; i--)
{
AdjustDown(i);
}
}

        void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
AdjustDown(0);
}

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

        const T& top()
{
return _con[0];
}

        bool empty()
{
return _con.empty();
}

        size_t size()
{
return _con.size();
}
private:
void AdjustDown(int parent)
{
Compare com;

            int child = parent * 2 + 1;
while (child < _con.size())
{
if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
{
child++;
}

                if (com(_con[parent], _con[child]))
{
swap(_con[parent], _con[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}

        void AdjustUp(int child)
{
Compare com;

            int parent = (child - 1) / 2;
while (child > 0)
{
if (com(_con[parent], _con[child]))
{
swap(_con[parent], _con[child]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}

}
}
Container _con;
};

    void test_priority_queue1()
{
// 默认是大堆 -- less
//priority_queue<int> pq;
//小堆
priority_queue<int,vector<int>,greater<int>> pq;

        pq.push(3);
pq.push(5);
pq.push(1);
pq.push(4);

        while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
}

2.仿函数/函数对象

template<class T, class Container = vector<T>, class Compare = less<T>>

优先级队列中的三个模板参数中,第三个传递的就是仿函数,可以自己定义实现仿函数的内容来控制优先级的变化。

仿函数本质上是一个类,该类定义了函数调用运算符 operator(),当创建该类的对象后,就可以像调用普通函数一样使用该对象。

对于上述的优先级队列,我们也可以自己实现仿函数,调用自己的比较器。

template<class T>
class Less
{
public:
bool operator()(const T& x, const T& y)
{
return x < y;
}
};
template<class T>
class Greater
{
public:
bool operator()(const T& x, const T& y)
{
return x > y;
}
};

http://www.xdnf.cn/news/19315.html

相关文章:

  • 论文介绍:《Small Language Models are the Future of Agentic AI》
  • Python(五)Python_C API详细
  • Linux三剑客grep-sed-awk
  • 为什么vue3会移除过滤器filter
  • 北斗导航 | RAIM算法改进方案及性能对比分析报告
  • 深度学习:洞察发展趋势,展望未来蓝图
  • 计算机网络面试集合
  • 【AI工具】在 VSCode中安装使用Excalidraw
  • Java全栈开发面试实战:从基础到微服务的深度解析
  • 小迪安全v2023学习笔记(七十四讲)—— 验证机制篇验证码绕过思路SRC挖掘演示
  • Coze源码分析-API授权-获取令牌列表-前端源码
  • LeetCode刷题记录----51.N皇后(Hard)
  • OpenCV安装及其开发环境配置(Windows系统Visual Studio 2022)
  • ros1ros2区别和相同之处
  • 软考 系统架构设计师系列知识点之杂项集萃(136)
  • 【LeetCode - 每日1题】有效的数独
  • Java基础知识(十)
  • plantsimulation知识点 多条RGV驮一台工件图标显示顺序问题
  • C语言类型转换踩坑解决过程
  • 重叠IO模型
  • 深入理解 Linux 驱动中的 file_operations:从 C 语言函数指针到类比 C++ 虚函数表
  • 学习Python中Selenium模块的基本用法(11:弹窗处理)
  • Day18_【机器学习—交叉验证与网格搜索】
  • 【ROS2】ROS2 基础学习教程 、movelt学习
  • PostgreSQL 数据库灾备要点与举例说明**
  • Spring Data Redis 的使用方法
  • 电子战:多功能雷达工作模式识别
  • [光学原理与应用-339]:ZEMAX - Spot Diagram(点列图)是评估光学系统成像质量的核心工具,它通过几何光线追迹直观展示像差对成像的影响。
  • 模拟实现STL中的list容器
  • 行内元素块元素