STL-priority_queue模拟实现
#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模拟实现的更多相关文章
- STL - priority_queue(优先队列)
优先级队列priority_queue 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用. priority_queue<int ...
- 详解C++ STL priority_queue 容器
详解C++ STL priority_queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(priority_queue\)容器的使用方法和常见的使用技巧. priority_queue容器 ...
- C++ 之STL priority_queue
priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional>Type 为数 ...
- <泛> STL - vector 模拟实现
今天为大家带来一个模拟STL-vector的模板实现代码. 首先看一下测试结果,之后再为大家呈现设计 测试效果 测试代码 #include<iostream> #include<ve ...
- 洛谷 P1739 表达式括号匹配【STL/stack/模拟】
题目描述 假设一个表达式有英文字母(小写).运算符(+,-,*,/)和左右小(圆)括号构成,以"@"作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返 ...
- 【转载】C++ STL priority_queue用法
priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为 ...
- STL priority_queue 常见用法详解
<算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...
- STL priority_queue 优先队列 小记
今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...
- STL priority_queue sort 自定义比较终极模板
比较有两种重载,一种是类内部的bool operator<( 只有一个参数 ),当然bool operator< 也可以拿到类的外面:另外一种是写一个cmp,利用cmp返回作为sort的第 ...
- C++STL priority_queue
priority_queue优先级队列 最大值优先级队列(队头是最大值) 最小值优先级队列(队头是最小值) priority_queue<int> q1;//默认定义为最大值优先级队列 ...
随机推荐
- 在web中,用户输入的文字过多 和页面排版一行显示不下 怎么办
在写管理系统中, 如果用户在input中,表单输入过多,应该如何去处理 常用的解决办法是: 1==>用户只能够输入一定范围内的字数 2==>超出几个字后(宽度)使用省略号显示. 3==&g ...
- 缩小ios的包体
不选全部兼容设备 在xcode中导出ipa时,不勾选导出全部兼容性设备,这样导出的ipa包含两种架构:armv7和64 打包压缩 unity提供三种压缩模式可以选择,默认选择的是:default不压缩 ...
- 虚拟IP绑定公网IP访问
绑定公网 IP 我们目前的虚拟 IP,还不能通过公网的形式进行访问,我们首先,来使用内部的 IP 进行访问看看效果如下: curl 虚拟IP 如上图我访问了两次,第一次访问返回的是 2222 的 ng ...
- Linux 统计Web服务日志命令
本人在Linux运维中收集的一些通用的统计,Apache/Nginx服务器日志的命令组合. Apache日志统计 # 列出当天访问次数最多的IP命令 [root@lyshark.cnblogs.com ...
- CentOS7设置防火墙
①查看防火状态 systemctl status firewalld service iptables status ②暂时关闭防火墙 systemctl stop firewalld service ...
- vue-router.esm.js:2065 Uncaught (in promise) Error: Redirected when going from "/login?redirect=%2Fhome" to "/home" via a navigation guard.
原因: vue-router路由版本更新产生的问题,导致路由跳转失败抛出该错误; 真正的原因是由于返回了一个Promise对象, 正常的跳转由then方法执行 当正常的路由跳转, 被"路 ...
- docker中的mysql时区修改
永久修改 进入容器 docker exec -it mysql5.7 bash 查看当前时区 date -R 修改时区 cp /usr/share/zoneinfo/PRC /etc/localtim ...
- Qt信号槽原理
1.说明 使用Qt已经好几年了,一直以为自己懂Qt,熟悉Qt,使用起来很是熟练,无论什么项目,都喜欢用Qt编写.但真正去看Qt的源码,去理解Qt的思想也就近两年的事. 本次就着重介绍一下Qt的核心功能 ...
- rpm安装卸载jdk
安装 rpm -ivh jdk-7-linux-x64.rpm 卸载 先查看安装的包 rpm -qa | grep jdk 卸载 rpm -e --nodeps jdk-1.7.0-fcs.x86_6 ...
- Advanced Installer傻瓜式打包教程
工具 Advanced Installer 11.0 前言 这个包不复杂,没有服务和注册表等操作,但需要.NET Framework 4.5和MySQL,同时需要初始化一下数据库,下面一起来实操一下. ...