标准模板库(STL)学习指南之priority_queue优先队列

简介: 转载自CSDN博客:http://blog.csdn.net/suwei19870312/article/details/5294016priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法实现,也算是堆的另外一种形式。

转载自CSDN博客:http://blog.csdn.net/suwei19870312/article/details/5294016

priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法
实现,也算是堆的另外一种形式。

先写一个用 STL 里面堆算法实现的与真正的STL里面的 priority_queue  用法相
似的 priority_queue, 以加深对 priority_queue 的理解

 

  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <vector>  
  4. using namespace std;  
  5. class priority_queue  
  6. {  
  7.     private:  
  8.         vector<int> data;  
  9.           
  10.     public:  
  11.         void push( int t ){   
  12.             data.push_back(t);   
  13.             push_heap( data.begin(), data.end());   
  14.         }  
  15.           
  16.         void pop(){  
  17.             pop_heap( data.begin(), data.end() );  
  18.             data.pop_back();  
  19.         }  
  20.           
  21.         int top()    { return data.front(); }  
  22.         int size()   { return data.size();  }  
  23.         bool empty() { return data.empty(); }  
  24. };  
  25.   
  26. int main()  
  27. {  
  28.     priority_queue  test;  
  29.     test.push( 3 );  
  30.     test.push( 5 );  
  31.     test.push( 2 );  
  32.     test.push( 4 );  
  33.       
  34.     while( !test.empty() ){  
  35.         cout << test.top() << endl;  
  36.         test.pop(); }  
  37.           
  38.     return 0;  
  39. }  

 

 

STL里面的 priority_queue 写法与此相似,只是增加了模板及相关的迭代器什么的。


priority_queue 对于基本类型的使用方法相对简单。
他的模板声明带有三个参数,priority_queue<Type, Container, Functional>
Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个
参数缺省的话,优先队列就是大顶堆,队头元素最大。
看例子

  1. #include <iostream>  
  2. #include <queue>  
  3. using namespace std;  
  4. int main(){  
  5.     priority_queue<int> q;  
  6.       
  7.     for( int i= 0; i< 10; ++i ) q.push( rand() );  
  8.     while( !q.empty() ){  
  9.         cout << q.top() << endl;  
  10.         q.pop();  
  11.     }  
  12.       
  13.     getchar();  
  14.     return 0;  
  15. }  
 
 
如果要用到小顶堆,则一般要把模板的三个参数都带进去。
STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆
例子:
  1. #include <iostream>  
  2. #include <queue>  
  3. using namespace std;  
  4. int main(){  
  5.     priority_queue<int, vector<int>, greater<int> > q;  
  6.       
  7.     for( int i= 0; i< 10; ++i ) q.push( rand() );  
  8.     while( !q.empty() ){  
  9.         cout << q.top() << endl;  
  10.         q.pop();  
  11.     }  
  12.       
  13.     getchar();  
  14.     return 0;  
  15. }  
 
 
对于自定义类型,则必须自己重载 operator< 或者自己写仿函数
先看看例子:
  1. #include <iostream>  
  2. #include <queue>  
  3. using namespace std;  
  4. struct Node{  
  5.     int x, y;  
  6.     Node( int a= 0, int b= 0 ):  
  7.         x(a), y(b) {}  
  8. };  
  9. bool operator<( Node a, Node b ){  
  10.     if( a.x== b.x ) return a.y> b.y;  
  11.     return a.x> b.x;   
  12. }  
  13. int main(){  
  14.     priority_queue<Node> q;  
  15.       
  16.     for( int i= 0; i< 10; ++i )  
  17.     q.push( Node( rand(), rand() ) );  
  18.       
  19.     while( !q.empty() ){  
  20.         cout << q.top().x << ' ' << q.top().y << endl;  
  21.         q.pop();  
  22.     }  
  23.       
  24.     getchar();  
  25.     return 0;  
  26. }  
 
 

自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
但此时不能像基本类型这样声明
priority_queue<Node, vector<Node>, greater<Node> >;
原因是 greater<Node> 没有定义,如果想用这种方法定义
则可以按如下方式

例子:

 

  1. #include <iostream>  
  2. #include <queue>  
  3. using namespace std;  
  4. struct Node{  
  5.     int x, y;  
  6.     Node( int a= 0, int b= 0 ):  
  7.         x(a), y(b) {}  
  8. };  
  9. struct cmp{  
  10.     bool operator() ( Node a, Node b ){  
  11.         if( a.x== b.x ) return a.y> b.y;  
  12.           
  13.         return a.x> b.x; }  
  14. };  
  15. int main(){  
  16.     priority_queue<Node, vector<Node>, cmp> q;  
  17.       
  18.     for( int i= 0; i< 10; ++i )  
  19.     q.push( Node( rand(), rand() ) );  
  20.       
  21.     while( !q.empty() ){  
  22.         cout << q.top().x << ' ' << q.top().y << endl;  
  23.         q.pop();  
  24.     }  
  25.       
  26.     getchar();  
  27.     return 0;  
  28. }  

 

 
目录
相关文章
|
6天前
|
弹性计算 人工智能 安全
云上十五年——「弹性计算十五周年」系列客户故事(第二期)
阿里云弹性计算十五年深耕,以第九代ECS g9i实例引领算力革新。携手海尔三翼鸟、小鹏汽车、微帧科技等企业,实现性能跃升与成本优化,赋能AI、物联网、智能驾驶等前沿场景,共绘云端增长新图景。
|
12天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
4天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
11天前
|
人工智能 自然语言处理 自动驾驶
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
|
7天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
637 17
|
6天前
|
人工智能 Java Nacos
基于 Spring AI Alibaba + Nacos 的分布式 Multi-Agent 构建指南
本文将针对 Spring AI Alibaba + Nacos 的分布式多智能体构建方案展开介绍,同时结合 Demo 说明快速开发方法与实际效果。
427 34
|
12天前
|
编解码 自然语言处理 文字识别
Qwen3-VL再添丁!4B/8B Dense模型开源,更轻量,仍强大
凌晨,Qwen3-VL系列再添新成员——Dense架构的Qwen3-VL-8B、Qwen3-VL-4B 模型,本地部署友好,并完整保留了Qwen3-VL的全部表现,评测指标表现优秀。
729 7
Qwen3-VL再添丁!4B/8B Dense模型开源,更轻量,仍强大