#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. el-dialog组件无法跟新视图上的数据

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%"> ...

  2. 紧跟潮流,抓住趋势,跟上全民AI的节奏,开源IM项目OpenIM产品介绍,为AIGC贡献力量

    开源价值 高度自主.安全可控.开放自由,私有化部署,一切皆可控 透明度和可信度:开源软件的源代码是公开的,任何人都可以查看和检查代码,从而增强了软件的透明度和可信度.用户可以了解软件的内部结构和运作方 ...

  3. 加快ios的出包速度

    在导出ipa时,通过这几种方法,可以更快地导出ipa来进行测试 不勾选bitcode 在导出ipa时,不勾选bitcode,这样会加快出包的速度,但导出来的ipa会大一些,关于bitcode可查看:& ...

  4. Mysql索引失效场景

    Mysql索引失效场景   序言   众所周知在Mysql中,通过使用索引的方式可以加快查询速度,从而避免全文搜索:而索引本身就像图书馆中所有书籍目录,通过查询关键字就能快速找到目标书籍在几列几行,这 ...

  5. 4.1 C++ STL 动态链表容器

    List和SList都是C++ STL中的容器,都是基于双向链表实现的,可以存储可重复元素的特点.其中,List内部的节点结构包含两个指针一个指向前一个节点,一个指向后一个节点,而SList只有一个指 ...

  6. 华为云DTSE助力车卫士架构升级,探索智能出行行业数字化新路径

    本文分享自华为云社区<华为云DTSE助力车卫士技术架构升级,探索智能出行行业数字化新路径>,作者:HuaweiCloudDeveloper. 毫无疑问,在双碳背景以及先进技术的加持下,智能 ...

  7. Gin 中间件

    中间件 在Gin框架中,中间件(Middleware)指的是可以拦截http请求-响应生命周期的特殊函数,在请求-响应生命周期中可以注册多个中间件,每个中间件执行不同的功能,一个中间执行完再轮到下一个 ...

  8. WebAssembly核心编程[4]: Memory

    由于Memory存储的是单纯的二进制字节,所以原则上我们可以用来它作为媒介,在wasm模块和数组程序之间传递任何类型的数据.在JavaScript API中,Memory通过WebAssembly.M ...

  9. .NET Core开发实战(第26课:工程结构概览:定义应用分层及依赖关系)--学习笔记

    26 | 工程结构概览:定义应用分层及依赖关系 从这一节开始进入微服务实战部分 这一节主要讲解工程的结构和应用的分层 在应用的分层这里定义了四个层次: 1.领域模型层 2.基础设施层 3.应用层 4. ...

  10. MySQL-CDC原理与实践

    MySQL CDC (Change Data Capture),中文名为MySQL变化数据捕获,是一种截取MySQL主从复制流中binlog的技术,从而实时捕获数据库中的增.删.改操作.在大数据.实时 ...