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;//默认定义为最大值优先级队列 ...
随机推荐
- Spring Boot日志框架Slf4j+logback
一.简介 Slf4j Java的简单日志记录外观(Simple Logging Facade for Java )可作为各种日志记录框架(例如java.util.logging,logback,log ...
- 从零开始配置vim(31)——git 配置
很抱歉又拖更了这么久了,在这个新公司我想快速度过试用期,所以大部分的精力主要花在日常工作上面.但是这个系列还是得更新下去,平时只能抽有限的业余时间来准备.这就导致我写这些文章就慢了一些. 废话不多说, ...
- go中x/sync/semaphore解读
semaphore semaphore的作用 如何使用 分析下原理 Acquire TryAcquire Release 总结 参考 semaphore semaphore的作用 信号量是在并发编程中 ...
- Linux文件IO之一 [补档-2023-07-21]
Linux文件IO 8-1C标准库IO函数的工作流程 使用fopen函数打开一个文件,之后会返回一个FILE* fp指针,fp指针指向一个结构体,这个结构体是c 标准io库中的一个结构体,这个结构 ...
- 【STL源码剖析】list模拟实现 | 适配器实现反向迭代器【超详细的底层算法解释】
今天博主继续带来STL源码剖析专栏的第三篇博客了! 今天带来list的模拟实现!话不多说,直接进入我们今天的内容! 前言 那么这里博主先安利一下一些干货满满的专栏啦! 手撕数据结构https://bl ...
- 【Linux】常用基本指令大汇总系列(篇一)【超详细的图解保姆教程】
常用基本指令大汇总系列(篇一) 大家好,欢迎大家来到我的博客.从今天开始,博主就要开启一个全新的系列了!Linux操作系统常用基本指令汇总系列,当大家按顺序学完这个系列,在Linux上编写基本的C语言 ...
- Google_Book_20Things.前言以及前四项学习笔记
20 THINGS I LEARNED ABOUT BROWSERS AND THE WEB Illustrated by Christoph Niemann. Written by the Goog ...
- 国产数据库TiDB初体验:简单易用,快速上手
最近开始关注国产数据库的发展,为了能从技术人员的角度来实际体验国产中目前最流行的TiDB数据库,从今天起,在官方公布的课程开始正面了解TiDB的设计理念. 看了2小时的入门课程介绍,总体来说,还是有不 ...
- Pandas—read_csv()/read_table()文本文件的读取
对于CSV及txt后缀的文本文件,分别使用pandas模块中的read_csv函数和read_table函数 文件类型 函数名称 CSV read_csv() txt read_table() 1. ...
- CF1295
A 用计算器式显示数字,可以显示 \(n\) 段.可以显示的最大数字是多少? 如果用了一个需要至少四段的数字,一定不如把这个替换成两个 \(1\) 好. 如果一共可以用偶数个,一定是全部 \(1\). ...