这篇文章主要介绍堆(最大堆和最小堆),以及一些系统对一些任务,比如线程,进程做调度的时候,所采用的优先级队列。

主要思想就是,做一个最大堆(任务的权重最大的在顶端),把顶端的任务取出,重新做一个堆,处理该任务。

// 优先级队列.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的更多相关文章

  1. STL中的优先级队列priority_queue

    priority_queue(queue类似)完全以底部容器为根据,再加上二叉堆(大根堆或者小根堆)的实现原理,所以其实现非常简单,缺省情况下priority_queue以vector作为底部容器.另 ...

  2. STL学习系列七:优先级队列priority_queue容器

    1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...

  3. C++ STL 学习笔记__(6)优先级队列priority_queue基本操作

    10.2.7优先级队列priority_queue v  最大值优先级队列.最小值优先级队列 v  优先级队列适配器 STL priority_queue v  用来开发一些特殊的应用,请对stl的类 ...

  4. c++ 优先级队列(priority_queue)

    从网上搜优先级队列用法,都是有些乱七八糟的,有几种用法都没说,直接贴代码.实在郁闷,于是自己在此归纳归纳. 废话不多说,直入主题. 优先级队列的核心是比较函数的实现. 比较函数有两种实现方法: 1.在 ...

  5. STL中的单向队列queue

    转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...

  6. C++ - 库函数优先级队列(priority_queue)输出最小值 代码

    库函数优先级队列(priority_queue)输出最小值 代码 本文地址: http://blog.csdn.net/caroline_wendy 库函数优先级队列(priority_queue)的 ...

  7. STL之优先级队列priority_queue

    摘要: priority_queue,自适应容器(即容器适配器):不能由list来组建: 最大值优先级队列(最大值始终在对首,push进去时候) 最小值优先级队列: 优先级队列适配器 STL  pri ...

  8. java中PriorityQueue优先级队列使用方法

    优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...

  9. 《转》JAVA中PriorityQueue优先级队列使用方法

    该文章转自:http://blog.csdn.net/hiphopmattshi/article/details/7334487 优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最 ...

随机推荐

  1. java int和String类型之间的相互转换

    String --> int 第一种方法:int i = Integer.parseInt(s); 第二种方法:int i = Integer.valueOf(s).intValue(); 两种 ...

  2. C#缓存的一点想法及测试

    项目开发中,用到了缓存,其中的一个列表项,可能要多线程处理,就有了下面的想法,具体的问题在代码中有详细说明,见下文. static void Main(string[] args) { 测试缓存的想法 ...

  3. python中使用多继承

    python中使用多继承,会涉及到查找顺序(MRO).重复调用(钻石继承,也叫菱形继承问题)等 MRO MRO即method resolution order,用于判断子类调用的属性来自于哪个父类.在 ...

  4. Eclipse下建立geoserver源码工程

    摘要:本文详细阐述,如何基于geoserver源码构建eclipse工程文件,操作过程中除用到jdk.eclipse以外,还有git和maven,操作系统为windows8. 1安装Git 从(htt ...

  5. openstack kilo 流量

  6. 可迭代的集合类型使用foreach语句

    在学习算法这本书图论那一部分的时候,接触到了几个类似for(int w:G.adj(v)),的语句,不是很理解,就去百度,发现这是一种叫做foreach的语法,在书的76页有讲到,但是之前没认真看书, ...

  7. LightOJ 1245 Harmonic Number (II)(找规律)

    http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS    ...

  8. 摄影初学者挑选相机的常见问题 FAQ

    数码相机一次次降价,越来越多的人加入摄影的行列,照相器材还是一个比较专业的领域,并非简单的参数比一下高低就可以知道好坏,很多朋友往往了解了好久还没弄清孰优孰劣,在购机前踌躇半天拿不定主意,我收集了被问 ...

  9. python简单网络服务器

    对于服务器来说建立TCP连接的过程分为4步: 1.建立socket对象:这里与客户端一样,依然是: s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) ...

  10. 如何在Html的CSS中去除<li>标签前面小黑点,和ul、LI部分属性方法

    div是很多人做网站都会用到的,但在显示效果时前面总是会有一个小黑点,这个效果很多人不想要,但又不知到如何去除,然而我们可以用以下方法来清除. 1.在CSS中写入代码.找到相关性的CSS,在..li和 ...