STL中的优先级队列(priority_queue)的自己实现priqueue
这篇文章主要介绍堆(最大堆和最小堆),以及一些系统对一些任务,比如线程,进程做调度的时候,所采用的优先级队列。
主要思想就是,做一个最大堆(任务的权重最大的在顶端),把顶端的任务取出,重新做一个堆,处理该任务。
// 优先级队列.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <functional>
#include <iostream>
using namespace std; template<typename T , class FuncCmp = std::less<T> >
class priqueue
{
T * x;
int n;
int maxsize;
FuncCmp comp;
private:
inline void swap(T & l , T & r)
{
T tmp = l;
l = r;
r = tmp;
} public:
void shiftup(int end)//将满足comp的元素,从底部提到顶部
{
//pre : [1,n-1]是堆
//post : [1,n]是堆
int i = end;
int p = i/2;
while( i != 1 && comp(x[i],x[p]) )
{
swap(x[i],x[p]);
i = p;
p = i / 2;
}
}
void shiftdown(int end)//将不满足comp的元素,从顶部往下移
{
//pre : [2,n]是堆
//post : [1,n]是堆
int i = 1;
int child;
while (1)
{
child = 2*i;
if (child > end)break;
if(child == end)
{
if ( !comp(x[i],x[child]) )
{
swap(x[i] , x[child]);
}
break;
}
if (child < end)
{
if ( !comp(x[child],x[child+1]) )
{
++child;
}
if ( !comp(x[i],x[child]) )
{
swap(x[i],x[child]);
}
else
{
break;
}
}
i = child;
}
}
void push_queue(T t)
{
if (n == maxsize)return; x[++n] = t;
shiftup(n);
}
T pop_queue()
{
if(n == 0)return T();
T ret = x[1];
x[1] = x[n--];
shiftdown(n);
x[n+1] = ret;
return ret;
}
void make_heap()
{
if(n == 0 || n==1)return;
for (int i = 2;i<=n ; ++i)
{
shiftup(i);
}
}
priqueue(int m)
{
maxsize = m;
n = 0;
x = new T[maxsize + 1];
}
~priqueue()
{
if (x)
{
delete x;
x = NULL;
}
}
int size()const
{
return n;
}
}; struct Tasks
{
int priority; struct OtherData
{
int data1;
int data2;
int data3;
int data4;
};
}; inline bool CompareTasks(Tasks * t1 , Tasks * t2)
{
return t1->priority < t2->priority;
} struct CompareTasksType
{
public:
bool operator()(Tasks * t1 , Tasks * t2)
{
return CompareTasks(t1,t2);
}
}; int _tmain(int argc, _TCHAR* argv[])
{
priqueue<Tasks * , CompareTasksType> q(12); Tasks t1; t1.priority = 12;
Tasks t2; t2.priority = 20;
Tasks t3; t3.priority = 15;
Tasks t4; t4.priority = 29;
Tasks t5; t5.priority = 23;
Tasks t6; t6.priority = 17;
Tasks t7; t7.priority = 22;
Tasks t8; t8.priority = 35;
Tasks t9; t9.priority = 40;
Tasks t10; t10.priority = 26;
Tasks t11; t11.priority = 51;
Tasks t12; t12.priority = 19; q.push_queue(&t1);
q.push_queue(&t2);
q.push_queue(&t3);
q.push_queue(&t4);
q.push_queue(&t5);
q.push_queue(&t6);
q.push_queue(&t7);
q.push_queue(&t8);
q.push_queue(&t9);
q.push_queue(&t10);
q.push_queue(&t11);
q.push_queue(&t12); //q.push_queue(12);
//q.push_queue(20);
//q.push_queue(15);
//q.push_queue(29);
//q.push_queue(23);
//q.push_queue(17);
//q.push_queue(22);
//q.push_queue(35);
//q.push_queue(40);
//q.push_queue(26);
//q.push_queue(51);
//q.push_queue(19); while(q.size() != 0)
{
Tasks * pT = q.pop_queue();
//do this task
//...
cout<<(pT->priority)<<endl;
} return 0;
}
STL中的优先级队列(priority_queue)的自己实现priqueue的更多相关文章
- STL中的优先级队列priority_queue
priority_queue(queue类似)完全以底部容器为根据,再加上二叉堆(大根堆或者小根堆)的实现原理,所以其实现非常简单,缺省情况下priority_queue以vector作为底部容器.另 ...
- STL学习系列七:优先级队列priority_queue容器
1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...
- C++ STL 学习笔记__(6)优先级队列priority_queue基本操作
10.2.7优先级队列priority_queue v 最大值优先级队列.最小值优先级队列 v 优先级队列适配器 STL priority_queue v 用来开发一些特殊的应用,请对stl的类 ...
- c++ 优先级队列(priority_queue)
从网上搜优先级队列用法,都是有些乱七八糟的,有几种用法都没说,直接贴代码.实在郁闷,于是自己在此归纳归纳. 废话不多说,直入主题. 优先级队列的核心是比较函数的实现. 比较函数有两种实现方法: 1.在 ...
- STL中的单向队列queue
转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...
- C++ - 库函数优先级队列(priority_queue)输出最小值 代码
库函数优先级队列(priority_queue)输出最小值 代码 本文地址: http://blog.csdn.net/caroline_wendy 库函数优先级队列(priority_queue)的 ...
- STL之优先级队列priority_queue
摘要: priority_queue,自适应容器(即容器适配器):不能由list来组建: 最大值优先级队列(最大值始终在对首,push进去时候) 最小值优先级队列: 优先级队列适配器 STL pri ...
- java中PriorityQueue优先级队列使用方法
优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...
- 《转》JAVA中PriorityQueue优先级队列使用方法
该文章转自:http://blog.csdn.net/hiphopmattshi/article/details/7334487 优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最 ...
随机推荐
- Ubuntu 16.04 TensorFlow CPU 版本安装
1.下载Anaconda,官方网站.我下载的时Python 2.7 64bit版本: 2.安装执行命令 bash Anaconda2-4.2.0-Linux-x86_64.sh 设置好目录后等 ...
- jsoup 获取指定页面的所有链接(需后续完善)
java代码如下: import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; impor ...
- 一个有趣的模拟光照的shader(类似法线贴图)
最近使用unity,碰到到一个很有趣的例子.场景无光线,却模拟出了光照,效果挺好.其思路与法线贴图原理异曲同工. 原作者提供的效果印象深刻. 模型除了使用原来的diffuse贴图外,还用到了一张模拟记 ...
- bzoj1036 树的统计Count
第一次写链剖,于是挑了个简单的裸题写. 以下几点要注意: 1.链剖中的height是从根到该店经过的轻边个数 2.分清num与sum..... #include<cstdio> #incl ...
- 多线程的Python 教程--“贪吃蛇”
本指南的里代码可以在这里下载: threadworms.py ,或者从 GitHub.代码需要 Python 3 或 Python 2 ,同时也需要安装 Pygame . 点击查看大版本图片 ...
- BITED数学建模七日谈之三:怎样进行论文阅读
前两天,我和大家谈了如何阅读教材和备战数模比赛应该积累的内容,本文进入到数学建模七日谈第三天:怎样进行论文阅读. 大家也许看过大量的数学模型的书籍,学过很多相关的课程,但是若没有真刀真枪地看过论文,进 ...
- HDU ACM 2121 Ice_cream’s world II (无根最小树形图)
[解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...
- 总结调用Flash的几种方法
一.Adobe 提供的方法 <object width="200" height="200" classid="clsid:D27CDB6E-A ...
- RHAS Linux下架构Lotus Domino详解(附视频)
此处下载操作视频:RHAS Linux下架构Lotus Domino 6.5视频教程 在rhas下架构Lotus Domino 汉化 650) this.width=650;" o ...
- ArcGIS Desktop10.2与CityEngine2012兼容问题
要培训ArcGIS Desktop和Esri CityEngine2012.在一台机器上装好Desktop10.2之后,在注册Esri CityEngine2012时报出了"7019:Inv ...