陕西省建设厅便民服务网站,网站首页怎么做ps,线上宣传有哪些好的方式方法,建设公司和建筑公司有什么区别1.priority_queque的介绍
1.priority_queue中文叫优先级队列。优先队列是一种容器适配器#xff0c;根据严格的弱排序标准#xff0c;它的第一个元素总是它所包含的元素中最大的。
2. 此上下文类似于堆#xff0c;在堆中可以随时插入元素#xff0c;并且只能检索最大堆元…1.priority_queque的介绍
1.priority_queue中文叫优先级队列。优先队列是一种容器适配器根据严格的弱排序标准它的第一个元素总是它所包含的元素中最大的。
2. 此上下文类似于堆在堆中可以随时插入元素并且只能检索最大堆元素(优先队列中位于顶部的元 素)。
3. 优先队列被实现为容器适配器容器适配器即将特定容器类封装作为其底层容器类queue提供一组特 定的成员函数来访问其元素。元素从特定容器的“尾部”弹出其称为优先队列的顶部。
4. 底层容器可以是任何标准容器类模板也可以是其他特定设计的容器类。
容器应该可以通过随机访问迭代器访问并支持以下操作
empty()检测容器是否为空
size()返回容器中有效元素个数
front()返回容器中第一个元素的引用
push_back()在容器尾部插入元素
pop_back()删除容器尾部元素
5. 标准容器类vector和deque满足这些需求。默认情况下如果没有为特定的priority_queue类实例化指定容器类则使用vector。
6. 需要支持随机访问迭代器以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数 make_heap、push_heap和pop_heap来自动完成此操作。
2.priority_queue的基本使用
优先级队列默认使用vector作为其底层存储数据的容器在vector上又使用了堆算法将vector中元素构造成堆的结构因此priority_queue就是堆所有需要用到堆的位置都可以考虑使用priority_queue。
注意 默认情况下priority_queue是大堆。
2.1构造函数
优先级队列的构造方式有两种直接构造一个空对象 和 通过迭代器区间进行构造
#include iostream
#include vector
#include queue //注意优先级队列包含在 queue 的头文件中using namespace std;int main()
{priority_queueint pq; //直接构造一个空对象默认为大堆cout typeid(pq).name() endl; //查看类型return 0;
}注意默认仿函数为less这个less决定了数据默认建大堆
通过迭代器区间构造对象
#include iostream
#include vector
#include queue //注意优先级队列包含在 queue 的头文件中using namespace std;int main()
{vectorchar vc { a,b,c,d,e };priority_queuechar, dequechar, greaterchar pq(vc.begin(), vc.end()); //现在是小堆cout typeid(pq).name() endl; //查看类型cout endl;while (!pq.empty()){//将小堆中的堆顶元素依次打印cout pq.top() ;pq.pop();}return 0;
}注意:
将比较方式改为 greater 后生成的是小堆
并且如果想修改比较方式的话需要指明模板参数2 底层容器
因为比较方式位于模板参数3不能跳跃缺省(遵循缺省参数规则) 2.2成员函数 #include iostream
#include vector
#include queue //注意优先级队列包含在 queue 的头文件中using namespace std;void Print(const priority_queueint pq)
{cout 是否为空 pq.empty() endl;cout 堆中的有效元素个数 pq.size() endl;cout 堆顶元素 pq.top() endl;cout endl;
}int main()
{vectorint v { 27,15,19,18,28,34,65,49,25,37 };priority_queueint pq(v.begin(), v.end()); //默认生成大堆Print(pq);pq.push(10);pq.push(100);Print(pq);pq.pop();pq.pop();pq.pop();Print(pq);return 0;
}2.3练习题 思路:
利用数组建立大小为 k 的小堆将剩余数据与堆顶值比较如果大于就入堆 为什么建小堆?因为此时需要的是最大的值建大堆可能会导致次大的值无法入堆
答案
class Solution {
public:int findKthLargest(vectorint nums, int k) {priority_queueint,vectorint,greaterint q(nums.begin(),nums.begin()k);for(int ik;inums.size();i){if(q.top()nums[i]){q.pop();q.push(nums[i]); }}return q.top();}
};
3.模拟实现优先级队列
3.1构造函数
优先级队列 priority_queue 属于容器适配器的一种像栈和队列一样没有迭代器
同时也不需要实现自己的具体功能调用底层容器的功能就行了
不过因为堆比较特殊需要具备 向上调整 和向下调整 的能力确保符合堆的规则
首先是基本框架
namespace bit
{templateclass T,class ComtainervectorT class Priority_Queue{public:Priority_Queue() {}templateclass InterIteratorprivate:Comtainer _con;};
}
接着是迭代器区间的默认构造
这个默认构造的功能是传递一段数据的迭代器区间给优先级队列然后优先级队列会自动在内部将数据排列成大堆或者小堆
namespace bit
{templateclass T,class ComtainervectorT class Priority_Queue{public:Priority_Queue() {}templateclass InterIteratorPriority_Queue(InterIterator first,InterIterator end){while (first ! end){_con.push_back(*first);/*将数据推送到容器内部*/first;}for (int i (_con.size() - 1 - 1) / 2; i 0; i--){Adjust_Down(i);}}private:Comtainer _con;};
} 3.2成员函数 bool empty()const/*判断是否为空复用底层结构的判空函数*/{return _con.empty();}const T top()const/*获取堆顶元素堆顶元素即第一个元素二叉树的祖宗*/{return _con[0];}size_t size()const/*获取优先级队列大小复用获取大小的函数*/{return _con.size();}
注意: 以上三个函数均为涉及对象内容的改变因此均使用 const 修饰 this 指针所指向的内容 在插入/删除数据后需要确保堆能符合要求
大堆:父节点比子节点大
小堆:父节点比子节点小
因此每进行一次数据修改相关操作都需要检查当前堆结构是否被破坏这一过程称为调整
插入数据:尾插数据然后向上调整 void Adjust_Up(int child)
{int parent child / 2 ;while(child0)if (_con[parent] _con[child]){std::swap(_con[parent], _con[child]);parent child;child parent-1/2;}elsebreak;
}void push(const T x){_con.push_back(x);Adjust_Up(_con.size()-1);}
删除数据:将堆顶数据交换至堆底删除堆底元素再向下调整堆
这步至关重要pop的时候将堆顶数据交换至堆底再通过容器的成员函数pop_back删除堆底的元素也就是删除掉容器存储的数据中最大或者最小的元素再从堆顶向下调整数据可以重新再这段数据中筛选出最大或者最小的元素推送到堆顶 void Adjust_Down(int parent){int child parent * 2 1;while (child _con.size()){if (child1_con.size()_con[child] _con[child 1]){child;}if (_con[parent] _con[child]){std::swap(_con[parent], _con[child]);parent child;child parent * 2 1;}elsebreak;}}void pop()const{std::swap(_con[0], _con[_con.size() - 1]);_con.pop_back();Adjust_Down(0);}
测试 3.3仿函数
仿函数又名函数对象 function obiects
需要调用库#includefunctional
仿函数的主要作用是
借助类和运算符重载做到同一格式兼容所有函数
这有点像函数指针
相比于函数指针又长又难理解的定义仿函数的使用可谓是很简单了
下面是两个仿函数作用是比较大小 templateclass Tclass Less{public:bool operator()(const T a, const T b){return a b;}};templateclass Tclass Greater{public:bool operator()(const T a, const T b){return a b;}};
此时 priority_queue 中的模板参数升级为3个而参数3的缺省值就是Less
templateclass T,class ComtainervectorT,class CompareGreaterT 像这样用仿函数生成的对象将所有要用到比较大小的代码进行替换
Compare greater;
void Adjust_Down(int parent)
{int child parent * 2 1;while (child _con.size()){if (child1_con.size()greater(_con[child] , _con[child 1])){child;}if (greater(_con[parent] , _con[child])){std::swap(_con[parent], _con[child]);parent child;child parent * 2 1;}elsebreak;}
}
这样就能实现排序顺序的自由控制 3.4特殊情况
在日期类中
class Date
{
public:Date(int year 1970, 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 std::ostream operator(std::ostream _cout, const Date d){_cout d._year - d._month - d._day;return _cout;}
private:int _year;int _month;int _day;
};此时会随机排序因为我们传入的是指针 地址是随机的所以结果也是随机的 所以这里我们专门为它构造一个仿函数就可以了
//小于
templateclass T
struct LessPDate
{bool operator()(const T p1, const T p2){return (*p1) (*p2);}
};//大于
templateclass T
struct GreaterPDate
{bool operator()(const T p1, const T p2){return (*p1) (*p2);}
};4.源码
源码链接 文章转载自: http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.rhkq.cn.gov.cn.rhkq.cn http://www.morning.wmdqc.com.gov.cn.wmdqc.com http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.lfgql.cn.gov.cn.lfgql.cn http://www.morning.w58hje.cn.gov.cn.w58hje.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.qwgct.cn.gov.cn.qwgct.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn http://www.morning.dodoking.cn.gov.cn.dodoking.cn http://www.morning.lxkhx.cn.gov.cn.lxkhx.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.fnwny.cn.gov.cn.fnwny.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.drfrm.cn.gov.cn.drfrm.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.crkhd.cn.gov.cn.crkhd.cn http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.jpqmq.cn.gov.cn.jpqmq.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.kkjhj.cn.gov.cn.kkjhj.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.kwyq.cn.gov.cn.kwyq.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.mnwb.cn.gov.cn.mnwb.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn