#include<deque> //测试用
#include<vector>//测试用
#include"9Date.h"//测试用
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
namespace test
{ template <class T >
struct less
{
bool operator()(const T& x, const T& y)
{
return x < y;
}
}; template<class T>
struct greater
{
bool operator()(const T& x, const T& y)
{
return x > y;
}
}; template<class T, class Containers = std::vector<T>, class Compare = less<T> >
class priority_queue
{
private:
Containers _con;
Compare _com;//或者换成匿名对象,感觉更爽
public:
void adjust_up(int child)
{
/**
* 向上调整算法(大堆)
* 计算父亲下标
* 如果孩子大于父亲,交换孩子和父亲,计算新父亲下标
* 如果孩子小于父亲,结束循环
* 相等换不换都行
*
*
*/ /**
* 计算堆的parent方法:
* 写一个有序值为下标的数组小堆,0-4五个数刚好,计算3和4通过什么方法得到1
*
* 0
* 1 2
* 3 4
*
* (4-1)/2 = 3/2 = 1
* 4/2 - 1 = 2-1 = 1
* (3-1)/2 = 2/2 = 1
* 3/2 - 1 = 1-1 = 0;
* 所以parent = (child-1)/2
*/ int parent = (child - 1) / 2;
while (child > 0) //child为0就不用再换了
{
if (_com(_con[parent] ,_con[child]))
//if (Compare()(_con[parent] ,_con[child]))
{
swap(_con[parent], _con[child]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
} }
void push(const T& x)
{
_con.push_back(x);
adjust_up(_con.size()-1);
}
void adjust_down(int parent)
{ /**
* 向下调整算法(大堆):
* 计算孩子下标
* 计算最大的孩子
* 比较孩子和父亲
* 如果父亲小于最大孩子,交换父亲和孩子,计算新孩子
* 如果父亲大于于最大孩子,结束循环
* 相等换不换都行
*
* 循环条件:左孩子下标不能超过数组大小
*
*
*/ /**
* 计算堆的child方法
* 写一个有序值为下标的数组小堆,0-4五个数刚好,计算
* 0
* 1 2
* 3 4
* 0*2+1 = 0+1 = 1 left
* 0*2+2 = 0+2 = 2 right
* 1*2+1 = 2+1 = 3 left
* 1*2+2 = 2+2 = 4 right
* 所以 left child = parent*2 + 1;
* 所以 right child = parent*2 + 2;
* 实际 right_child = left_child+1
*/ //方法一
/*
int left_child = parent * 2 + 1;
//int right_child = parent * 2 + 2;
int right_child = left_chile + 1;
int min_child = left_child; while (left_child < _con.size() )
//不能限制右孩子,在没有右孩子,但有左孩子更小的情况,parent需要和左孩子交换,如果右孩子限制了,那可能会丢失一个左孩子
{
if (right_child < _con.size() && left_child > right_child)
{
min_child = right_child;
}
if (_con[parent] < _con[child])
{
swap(_con[parent], _con[min_child]);
left_child = parent * 2 + 1;
//right_child = parent * 2 + 2;
right_child = left_child + 1;
min_child = left_child;
}
else
{
break;
}
}
*/
///优化方案
size_t child = parent * 2 + 1;
while (child < _con.size())
{
//if ((child + 1 )< _con.size() && _con[child]<_con[child + 1] )
if ((child + 1 )< _con.size() && _com(_con[child],_con[child + 1]) )
{
child = child + 1;
}
if (_com(_con[parent] , _con[child]))
{
swap(_con[parent], _con[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
} }
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
adjust_down(0);
}
const T& top()
{
return _con[0];
}
bool empty()
{
return _con.empty();
}
size_t size()
{
return _con.size();
} }; //测试用例 void test_priority_queue1()
{
priority_queue<int , std::deque<int>,greater<int>> pq;
//priority_queue<int> pq;
pq.push(2);
pq.push(4);
pq.push(1);
pq.push(3);
pq.push(6);
pq.push(5); while (!pq.empty())
{
cout << pq.top() << endl;
pq.pop();
}
} //struct DateLess //这个不需要重载,我们在上方写了模板,参数T可以支持了
//{
// bool operator()(const Date& x, const Date& y)
// {
// return x < y;
// }
//};
struct PDateLess
{
bool operator()(const Date* p1, const Date* p2)
{
return (*p1) < (*p2);
}
};
void test_priority_queue2()
{
//priority_queue<Date> pq1;
//pq1.push(Date(2001, 1, 1));
//pq1.push(Date(2001, 1, 2));
//pq1.push(Date(2001, 1, 3));
//cout << pq1.top() << endl;
cout << "=================" << endl;
priority_queue<Date*,std::vector<Date*>,PDateLess> pq2; //显式指定才会调用PDateLess,不然会调用模板的用于比较地址
pq2.push(new Date(2001, 1, 1));
pq2.push(new Date(2001, 1, 2));
pq2.push(new Date(2001, 1, 3));
cout << *(pq2.top()) << endl;
} }

STL-priority_queue模拟实现的更多相关文章

  1. STL - priority_queue(优先队列)

    优先级队列priority_queue 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用. priority_queue<int ...

  2. 详解C++ STL priority_queue 容器

    详解C++ STL priority_queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(priority_queue\)容器的使用方法和常见的使用技巧. priority_queue容器 ...

  3. C++ 之STL priority_queue

    priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional>Type 为数 ...

  4. <泛> STL - vector 模拟实现

    今天为大家带来一个模拟STL-vector的模板实现代码. 首先看一下测试结果,之后再为大家呈现设计 测试效果 测试代码 #include<iostream> #include<ve ...

  5. 洛谷 P1739 表达式括号匹配【STL/stack/模拟】

    题目描述 假设一个表达式有英文字母(小写).运算符(+,-,*,/)和左右小(圆)括号构成,以"@"作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返 ...

  6. 【转载】C++ STL priority_queue用法

    priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为 ...

  7. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  8. STL priority_queue 优先队列 小记

    今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...

  9. STL priority_queue sort 自定义比较终极模板

    比较有两种重载,一种是类内部的bool operator<( 只有一个参数 ),当然bool operator< 也可以拿到类的外面:另外一种是写一个cmp,利用cmp返回作为sort的第 ...

  10. C++STL priority_queue

    priority_queue优先级队列 最大值优先级队列(队头是最大值)  最小值优先级队列(队头是最小值) priority_queue<int> q1;//默认定义为最大值优先级队列 ...

随机推荐

  1. 让一段代码执行在new Vue之前

    这是一个自调用函数,也有人叫做一次性函数: 这样函数前面最后打一个: ;(function initApp(){ loadApp(); })() function loadApp (){ //tena ...

  2. Python 潮流周刊#12:Python 中如何调试死锁问题?

    查看全文: https://pythoncat.top/posts/2023-07-22-weekly 文章&教程 1.使用 PyStack 调试 Python 中的崩溃和死锁 (英) 2.介 ...

  3. tortoisegit 还原远程分支到某个版本

    v2还原到v1 1.强制还原(git reset) 如果使用这种方式还原到v1,将丢失还原到v1到v2之间的所有提交及日志. 1.1显示日志 有save1.save2两条提交记录. 1.2 重置版本( ...

  4. 在K8S中,静态、动态、自主式Pod有何区别?

    在Kubernetes(简称K8s)中,静态Pod.自主式Pod和动态Pod是不同管理方式下的Pod类型,它们的区别主要体现在创建和管理方式上: 静态Pod: 静态Pod是由kubelet直接管理的, ...

  5. 使用rider调试lua

    emmylua1.3.5及以上版本支持rider调试,但emmylua的新版本只支持rider2020及以上版本,所以如果想用rider来调试lua,就要升级rider为2020,emmylua插件从 ...

  6. 微信小程序-双线程渲染模型

    微信小程序双线程渲染模型 小程序的运行环境分成渲染层和逻辑层: WXML 模板和 WXSS 样式工作在渲染层,通过 WebView 进行渲染 小程序会为每一个界面都创建一个 WebView 来渲染这个 ...

  7. 微信小程序-页面跳转数据传递

    在之前的文章当中我们都实现了一个功能就是可以从上一个页面传递数据给下一个页面,那么我们能不能从下一个页面传递数据给上一个页面呢,答案是可以的. 所以说本文这次主要介绍的内容就是返回上一个页面时传递参数 ...

  8. SqlSugar导航查询/多级查询

    1.导航查询特点 作用:主要处理主对象里面有子对象这种层级关系查询 1.1 无外键开箱就用 其它ORM导航查询 需要 各种配置或者外键,而SqlSugar则开箱就用,无外键,只需配置特性和主键就能使用 ...

  9. C/C++ Npcap包实现数据嗅探

    npcap 是Nmap自带的一个数据包处理工具,Nmap底层就是使用这个包进行收发包的,该库,是可以进行二次开发的,不过使用C语言开发费劲,在进行渗透任务时,还是使用Python构建数据包高效,这东西 ...

  10. ESXi6.5导入虚拟机提示缺少所需的磁盘镜像

    环境 esxi6.7 错误提示 解决方案 原因:这是因为导出虚拟机的时候,没有把"CD/DVD驱动器"删掉,在导入的时候,找不到这个磁盘映像. 编辑.ovf文件,找到ovf:hre ...