C++ 优先队列 使用STL容器
来源:互联网 发布:小甲鱼c语言视频教程 编辑:程序博客网 时间:2024/05/18 20:11
一、优先队列定义:priority_queue
普通的队列是一种先进先出的数据结构,元素在队尾追加,而从队头删除。
在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出 (largest-in,first-out)的行为特征。
优先队列不是直接将新元素放置在队列尾部,而是放在比它优先级低的元素前面。
标准库STL默认使用<操作符来确定对象之间的优先级关系,所以如果要使用自定义对象,需要重载 < 操作符。
优先队列有两种,最大优先队列和最小优先队列;每次从队列取出的的第一个元素分别是优先级最大和优先级最小的元素。
二、优先队列使用方式:
1.使用具有默认优先级的已有数据结构;
2.在定义优先队列的时候传入自定义的优先级比较对象;
3.使用自定义对象(数据结构),但是必须重载好< 操作符。
三、优先队列常用操作:
q.empty() 如果队列为空,则返回true,否则返回false
q.size() 返回队列中元素的个数
q.pop() 删除队首元素,但不返回其值
q.top() 返回具有最高优先级的元素值,但不删除该元素
q.push(item) 在基于优先级的适当位置插入新元素
四、优先队列源代码示例:#include "stdafx.h"#include<iostream>#include<functional>#include<queue>#include<vector>using namespace std;//定义比较结构struct Cmp_min_first{ bool operator ()(int &a,int &b){ return a>b;//最小值优先 }};struct Cmp_max_first{ bool operator ()(int &a,int &b){ return a<b;//最大值优先 }};//自定义数据结构struct Custom_min_first{ int x; bool operator < (const Custom_min_first &a) const { return x>a.x;//最小值优先 }};struct Custom_max_first{ int x; bool operator < (const Custom_max_first &a) const { return x<a.x;//最大值优先 }};int a[]={14,10,56,7,83,22,36,91,3,47,72,0};Custom_min_first num1[]={14,10,56,7,83,22,36,91,3,47,72,0};Custom_max_first num2[]={14,10,56,7,83,22,36,91,3,47,72,0};int _tmain(int argc, _TCHAR* argv[]){ priority_queue<int> que;//采用默认优先级构造队列//注意:以下“>>”中间必须要有空格,否则会被认为是错误 priority_queue<int,vector<int>,greater<int> > que1;//最小值优先队列 priority_queue<int,vector<int>,less<int> > que2;////最大值优先队列 priority_queue<int,vector<int>,Cmp_min_first> que3;//最小值优先队列 priority_queue<int,vector<int>,Cmp_max_first> que4;//最大值优先队列 priority_queue<Custom_min_first> que5; //最小优先级队列 priority_queue<Custom_max_first> que6; //最大优先级队列 int i;//入队 for(i=0;a[i];i++){ que.push(a[i]); que1.push(a[i]); que2.push(a[i]); que3.push(a[i]); que4.push(a[i]); } for(i=0;num1[i].x;i++) que5.push(num1[i]); for(i=0;num2[i].x;i++) que6.push(num2[i]); printf("一、采用默认优先关系:\npriority_queue<int> que;\n"); printf("Queue 0:\n"); while(!que.empty()){ printf("%3d",que.top()); que.pop(); } puts("\n"); printf("二、采用头文件\"functional\"内定义优先级:\npriority_queue<int,vector<int>,greater<int>/less<int> > que;\n"); printf("Queue 1:\n"); while(!que1.empty()){ printf("%3d",que1.top()); que1.pop(); } puts(""); printf("Queue 2:\n"); while(!que2.empty()){ printf("%3d",que2.top()); que2.pop(); } puts("\n"); printf("三、(1)采用结构体自定义优先级方式一:\npriority_queue<int,vector<int>,cmp> que;\n"); printf("Queue 3:\n"); while(!que3.empty()){ printf("%3d",que3.top()); que3.pop(); } puts(""); printf("Queue 4:\n"); while(!que4.empty()){ printf("%3d",que4.top()); que4.pop(); } puts("\n"); printf("三、(2)采用结构体自定义优先级方式二:\npriority_queue<custom> que;\n"); printf("Queue 5:\n"); while(!que5.empty()){ printf("%3d",que5.top()); que5.pop(); } puts(""); printf("Queue 6:\n"); while(!que6.empty()){ printf("%3d",que6.top()); que6.pop(); } puts(""); return 0;}
五、运行结果:
0 0
- C++STL之priority_queue优先队列容器
- C++STL优先队列使用
- C++ 优先队列 使用STL容器
- STL 容器 priority_queue(优先队列)
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器_优先队列
- c++STL中优先队列的使用
- c++STL中优先队列的使用
- 录制脚本时events无限增加
- JSF中怎么使用Enum生成下拉列表
- ubuntu 14.04 安装terminator后字体挤到一起解决方法
- 利用Python写的展示汉诺塔(hanoi)解法的小程序_Prj003
- js正则表达式的使用与详解
- C++ 优先队列 使用STL容器
- loadrunner安装注意事项
- 模拟DVD管理器(及遇到的问题与解决)
- 文章标题
- Android与设计模式:用单一职责则为Activity解耦
- sql server 2012 查询时间段月份数据方法
- 绘制图像直方图
- 解析nginx负载均衡
- mt4常用函数和常量