1 Star 0 Fork 0

xzx/C++学习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
priority_queue.h 3.52 KB
一键复制 编辑 原始数据 按行查看 历史
#pragma once
#include<assert.h>
namespace Mypriority_queue {
template<class T>
struct less {
bool operator()(const T& left, const T& right) const
{
return left < right;
}
};
template<class T>
struct greater {
bool operator()(const T& left, const T& right) const
{
return left > right;
}
};
template<class T, class Container = vector<T>,class Compare = less<T>>
class priority_queue
{
public:
//强制生成默认构造
priority_queue() = default;
template<class InputIterator>
priority_queue(InputIterator first, InputIterator last)
:_con(first, last)
{
//使用向下调整算法建堆
for (int i = (_con.size() - 2) / 2; i >= 0; i--)
{
AdjustDown(i);
}
}
void AdJustUp(int child)
{
int parent = (child - 1) / 2;
if (parent >= 0 && Compare()(_con[parent],_con[child])) {
std::swap(_con[parent], _con[child]);
AdJustUp(parent);
}
}
void push(const T& val)
{
_con.push_back(val);
AdJustUp(_con.size() - 1);
}
void AdjustDown(int parent)
{
int child = 2 * parent + 1;
if (child + 1 < _con.size() && Compare()(_con[child],_con[child + 1])) {
child++;
}
if (child < _con.size() && Compare()(_con[parent],_con[child])) {
std::swap(_con[child], _con[parent]);
AdjustDown(child);
}
}
void pop()
{
assert(!empty());
std::swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
AdjustDown(0);
}
const T& top() const
{
assert(!empty());
return _con[0];
}
bool empty() const
{
return _con.empty();
}
bool size() const
{
return _con.size();
}
void swap(priority_queue& q)
{
_con.swap(q._con);
}
private:
Container _con;
};
}
//namespace Mypriority_queue {
// template<class T>
// struct less {
// bool operator()(const T& left, const T& right) const
// {
// return left < right;
// }
// };
// template<class T>
// struct greater {
// bool operator()(const T& left, const T& right) const
// {
// return left > right;
// }
// };
// template<class T, class Container = vector<T>,class Compare = less<T>>
// class priority_queue
// {
// public:
// //强制生成默认构造
// priority_queue() = default;
//
// template<class InputIterator>
// priority_queue(InputIterator first, InputIterator last)
// :_con(first, last)
// {
// for (int i = (_con.size() - 2) / 2; i >= 0; i--)
// {
// AdjustDown(i);
// }
// }
// void AdJustUp(int child)
// {
// Compare com;
// int parent = (child - 1) / 2;
// if (parent >= 0 && com(_con[parent],_con[child])) {
// swap(_con[parent], _con[child]);
// AdJustUp(parent);
// }
// }
// void push(const T& val)
// {
// _con.push_back(val);
// AdJustUp(_con.size() - 1);
// }
// void AdjustDown(int parent)
// {
// Compare com;
// int child = 2 * parent + 1;
// if (child + 1 < _con.size() && com(_con[child] , _con[child+1])) {
// child++;
// }
// if (child<_con.size() && com(_con[parent],_con[child])) {
// swap(_con[child], _con[parent]);
// AdjustDown(child);
// }
// }
// void pop()
// {
// assert(!empty());
// swap(_con[0], _con[_con.size() - 1]);
// _con.pop_back();
// AdjustDown(0);
// }
// const T& top() const
// {
// assert(!empty());
// return _con[0];
// }
// bool empty() const
// {
// return _con.empty();
// }
// bool size() const
// {
// return _con.size();
// }
//
// private:
// Container _con;
// };
//}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/xie-zhus-shovel/c-learning.git
git@gitee.com:xie-zhus-shovel/c-learning.git
xie-zhus-shovel
c-learning
C++学习
master

搜索帮助