#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. Ant Design Vue中Table对齐方式显示省略号

    Ant Design Vue中Table对齐方式显示省略号 <template> <!-- bordered 表示表格中的边框 pagination="false" ...

  2. P9779_[HUSTFC 2023] 不定项选择题_题解

    rt 题目 有一道共 n 个选项的不定项选择题,它的答案至少包含一个选项,由于题目与选项的内容晦涩难懂,你打算通过尝试每一种可能的答案来通过这道题. 初始时所有选项都没有被勾选,你可以执行任意次下述操 ...

  3. springboot集成swagger之knife4j实战(升级版)

    官方文档链接:https://doc.xiaominfo.com/ 一.Knifej和swagger-bootstrap-ui对比 Knife4j在更名之前,原来的名称是叫swagger-bootst ...

  4. 【编写环境一】遇到常见python函数处理方式

    1.python实现两个一维列表合并成一个二维列表 >>> list1 = [1,2,3,4,4] >>> list2 = [2,3,4,5,2] >> ...

  5. 从嘉手札<2023-11-27>

    "我也没做错什么,放它去看海,总比跟着我好" 很多时候,悲伤总是细细的钻进心底 悄悄的生根发芽 待到了时机 它便如同一株参天巨树般郁郁葱葱 郁郁葱葱的令人发疯 人生本就像是做了一场 ...

  6. Advanced Installer傻瓜式打包教程

    工具 Advanced Installer 11.0 前言 这个包不复杂,没有服务和注册表等操作,但需要.NET Framework 4.5和MySQL,同时需要初始化一下数据库,下面一起来实操一下. ...

  7. Windows 10 ISO原版镜像文件下载(2024年01月)

    Windows 10 (business editions), version 22H2 (x64) - DVD (Chinese-Simplified) 链接:https://pan.baidu.c ...

  8. Linux-cp命令常用选项

    cp 命令是 Linux 中一个重要的命令,你可能经常会用到它.正如名称所示,cp 代表 复制(copy),它被用于在 Linux 命令行中复制文件和目录. 语法格式 mv [选项] 源文件或目录 目 ...

  9. [Java]BigDecimal与Double的区别和使用场景

    BigDecimal与Double的区别和使用场景 背景 在项目中发现开发小组成员在写程序时,对于Oracle数据类型为Number的字段(经纬度),实体映射类型有的人用Double有的人用BigDe ...

  10. UVA12390 Distributing Ballot Boxes 题解

    题目传送门 题意 有 \(n\) 个城市,\(b\) 个投票箱,第 \(i\) 个城市有 \(a_i\) 人,每个人均有一张票,将 \(b\) 个投票箱分给 \(n\) 个城市,每个城市的票分摊在投票 ...