上饶做网站哪家好哦,用html5做网站的优点,黑龙江省建设信息网,具有价值的响应式网站priority_queue的相关介绍
优先级队列是一种容器适配器#xff0c;根据严格的排序标准#xff0c;它的第一个元素总是它所包含的元素中最大(小)的。该容器适配器类似于堆#xff0c;在堆中可以随时插入元素#xff0c;并且可以检索最大(小)堆元素(优先级队列中位于顶部的元…priority_queue的相关介绍
优先级队列是一种容器适配器根据严格的排序标准它的第一个元素总是它所包含的元素中最大(小)的。该容器适配器类似于堆在堆中可以随时插入元素并且可以检索最大(小)堆元素(优先级队列中位于顶部的元素)。优先级队列被实现为容器适配器容器适配器即 将特定容器类封装作为其底层容器类queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出其称为优先级队列的顶部。底层容器可以是任何标准容器类模板也可以是其他特定设计的容器类。容器应可以通过随机访问迭代器访问。标准容器类vector和deque皆满足这些需求。默认情况下如果没有为特定的priority_queue类实例化指定容器类则使用vector。
更多关于priority_queue的详细内容请点击priority_queue的文档介绍
priority_queue的使用 优先级队列默认使用vector作为其底层存储数据的容器在vector上又使用了堆算法将vector中元素构造成堆的结构因此priority_queue就是堆所有需要用到堆的位置都可以考虑使用priority_queue。注意默认情况下priority_queue是大堆。 函数声明接口说明 priority_queue() priority_queue(InputIterator first, InputIterator last) 无参构造 迭代器区间初始化构造 empty()检查优先级队列是否为空top()返回优先级队列中最大(最小元素)即堆顶元素push(x)在优先级队列中插入元素xpop()删除优先级队列中最大(最小)元素即堆顶元素
// 使用举例(和queue类似)
int arr[] { 3,2,7,6,0,4,1,9,8,5 };
// 迭代器区间初始化
priority_queueint pq1(arr, arr sizeof(arr) / sizeof(arr[0]));while (!pq1.empty()) // 判断优先级队列是否为空
{cout pq1.top() ; // 获取栈顶元素pq1.pop(); // 删除元素
}
cout endl;
// 结果为9,8,7,6,5,4,3,2,1,0
上述代码结果默认是大堆(降序)其默认仿函数为less()若想得到升序序列只需改变仿函数为greater()即可。
// 改变仿函数
priority_queueint, vectorint, greaterint pq1(arr, arr sizeof(arr) / sizeof(int));
priority_queue的底层实现
ps. 默认情况下创建的是大堆其底层按照小于号比较
// 迭代器区间初始化
priority_queue(InputIterator first, InputIterator last)
{while (first ! last){_con.push_back(*first);first;}//建堆for (int i (_con.size() - 1 - 1) / 2; i 0; i--){AdjustDown(i); //向下调整算法}
}
// 向上调整算法
void AdjustUp(int child)
{int parent (child - 1) / 2;while (child 0){// 使用仿函数if (_comFunc(_con[parent], _con[child])){swap(_con[parent], _con[child]);child parent;parent (child - 1) / 2;}elsebreak;}
}
// 插入
void push(const T x)
{_con.push_back(x);AdjustUp(_con.size() - 1);
}
// 向下调整算法
void AdjustDown(int parent)
{size_t child parent * 2 1;while (child _con.size()){// 使用仿函数if (child 1 _con.size() _comFunc(_con[child], _con[child 1])){child;}if (_comFunc(_con[parent], _con[child])){swap(_con[parent], _con[child]);parent child;child parent * 2 1;}elsebreak;}
}
//删除
void pop()
{swap(_con[0], _con[_con.size() - 1]);_con.pop_back();AdjustDown(0);
} 文章转载自: http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.msfqt.cn.gov.cn.msfqt.cn http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn http://www.morning.zmpqt.cn.gov.cn.zmpqt.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.zxwqt.cn.gov.cn.zxwqt.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.pskjm.cn.gov.cn.pskjm.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.lctrz.cn.gov.cn.lctrz.cn http://www.morning.dlrsjc.com.gov.cn.dlrsjc.com http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.nqwz.cn.gov.cn.nqwz.cn http://www.morning.pghfy.cn.gov.cn.pghfy.cn http://www.morning.spsqr.cn.gov.cn.spsqr.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.nmfml.cn.gov.cn.nmfml.cn http://www.morning.qtryb.cn.gov.cn.qtryb.cn http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.kwpnx.cn.gov.cn.kwpnx.cn http://www.morning.gnfkl.cn.gov.cn.gnfkl.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.xlbyx.cn.gov.cn.xlbyx.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.glxmf.cn.gov.cn.glxmf.cn http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.dnqliv.cn.gov.cn.dnqliv.cn http://www.morning.fwmln.cn.gov.cn.fwmln.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.qytby.cn.gov.cn.qytby.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn