网站设计机构图,天噜啦更换域名解析,天河做网站系统,tor网站建设#x1f4d8;北尘_#xff1a;个人主页 #x1f30e;个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上#xff0c;不忘来时的初心 文章目录 一、priority_queue的介绍和使用1、priority_queue的介绍2、priority_queue的使用 二、priori… 北尘_个人主页 个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上不忘来时的初心 文章目录 一、priority_queue的介绍和使用1、priority_queue的介绍2、priority_queue的使用 二、priority_queue的模拟实现1、无仿函数2、带仿函数 一、priority_queue的介绍和使用
1、priority_queue的介绍
优先队列是一种容器适配器根据严格的弱排序标准它的第一个元素总是它所包含的元素中最大的。此上下文类似于堆在堆中可以随时插入元素并且只能检索最大堆元素(优先队列中位于顶部的元 素)。优先队列被实现为容器适配器容器适配器即将特定容器类封装作为其底层容器类queue提供一组特 定的成员函数来访问其元素。元素从特定容器的“尾部”弹出其称为优先队列的顶部。底层容器可以是任何标准容器类模板也可以是其他特定设计的容器类。容器应该可以通过随机访问迭 代器访问并支持以下操作 empty()检测容器是否为空 size()返回容器中有效元素个数 front()返回容器中第一个元素的引用 push_back()在容器尾部插入元素 pop_back()删除容器尾部元素标准容器类vector和deque满足这些需求。默认情况下如果没有为特定的priority_queue类实例化指 定容器类则使用vector。需要支持随机访问迭代器以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数 make_heap、push_heap和pop_heap来自动完成此操作。
2、priority_queue的使用
优先级队列默认使用vector作为其底层存储数据的容器在vector上又使用了堆算法将vector中元素构造成 堆的结构因此priority_queue就是堆所有需要用到堆的位置都可以考虑使用priority_queue。注意 默认情况下priority_queue是大堆。
默认情况下priority_queue是大堆 如果要建小堆需要改变条件 如果在priority_queue中放自定义类型的数据用户需要在自定义类型中提供 或者 的重载。
class Date
{
public:Date(int year 1900, int month 1, int day 1): _year(year), _month(month), _day(day){}bool operator(const Date d)const{return (_year d._year) ||(_year d._year _month d._month) ||(_year d._year _month d._month _day d._day);}bool operator(const Date d)const{return (_year d._year) ||(_year d._year _month d._month) ||(_year d._year _month d._month _day d._day);}friend ostream operator(ostream _cout, const Date d){_cout d._year - d._month - d._day;return _cout;}
private:int _year;int _month;int _day;
};
void TestPriorityQueue()
{// 大堆需要用户在自定义类型中提供的重载priority_queueDate q1;q1.push(Date(2018, 10, 29));q1.push(Date(2018, 10, 28));q1.push(Date(2018, 10, 30));cout q1.top() endl;// 如果要创建小堆需要用户提供的重载priority_queueDate, vectorDate, greaterDate q2;q2.push(Date(2018, 10, 29));q2.push(Date(2018, 10, 28));q2.push(Date(2018, 10, 30));cout q2.top() endl;}
二、priority_queue的模拟实现
1、无仿函数
namespace bit
{templateclass T , class Container vectorintclass priority_queue{public:void adjust_up(int child){int parent (child - 1) / 2;while (child 0){if (_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){int child parent * 2 1;while (child _con.size()){if (child 1 _con.size() _con[child 1] _con[child]){child;}if (_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);}bool empty(){return _con.empty();}const T top(){return _con[0];}private:Container _con;};
}2、带仿函数
namespace bit
{templateclass T , class Container vectorint,class CompareLessTclass priority_queue{public:void adjust_up(int child){Compare com;int parent (child - 1) / 2;while (child 0){if (com(_con[parent], _con[child])){swap(_con[child], _con[parent]);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){Compare com;size_t child parent * 2 1;while (child _con.size()){if (child 1 _con.size() com(_con[child], _con[child 1])){child;}if (com(_con[parent], _con[child])){swap(_con[child], _con[parent]);parent child;child parent * 2 1;}else{break;}}}void pop(){swap(_con[0], _con[_con.size() - 1]);_con.pop_back();adjust_down(0);}bool empty(){return _con.empty();}const T top(){return _con[0];}private:Container _con;};}int main()
{bit::priority_queueint,vectorint,Lessint v;v.push(1);v.push(9);v.push(8);while (!v.empty()){cout v.top() ;v.pop();}return 0;
} 文章转载自: http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.mksny.cn.gov.cn.mksny.cn http://www.morning.ttaes.cn.gov.cn.ttaes.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.jqmmf.cn.gov.cn.jqmmf.cn http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.qxwwg.cn.gov.cn.qxwwg.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.swkzr.cn.gov.cn.swkzr.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.wkknm.cn.gov.cn.wkknm.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.rfbpq.cn.gov.cn.rfbpq.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.lrylj.cn.gov.cn.lrylj.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.hongjp.com.gov.cn.hongjp.com http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.bqqzg.cn.gov.cn.bqqzg.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.nsppc.cn.gov.cn.nsppc.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.zmbzl.cn.gov.cn.zmbzl.cn http://www.morning.nkpml.cn.gov.cn.nkpml.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.hsxkq.cn.gov.cn.hsxkq.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.gcszn.cn.gov.cn.gcszn.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.zkzjm.cn.gov.cn.zkzjm.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.dhyzr.cn.gov.cn.dhyzr.cn http://www.morning.nytpt.cn.gov.cn.nytpt.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.txgjx.cn.gov.cn.txgjx.cn http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.zhqfn.cn.gov.cn.zhqfn.cn