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

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

// 优先级队列.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. SQL和NOSQL有区别吗?

    在大数据高速发展的今天,数据量在不断的增加,传统的数据库可能不能满足人们的需求了,这个时候新霸哥注意到了NOSQL出现了可以解决这个问题.我们知道sql数据库可以存储数据和处理数据,但是NOSQL最大 ...

  2. java webservice的多种实现方法汇总

    一.基于EJB容器管理webservice :     1.首先建立一个Web services EndPoint: package cn.test.service.impl; import java ...

  3. delphi请求idhttp数据

    idhttp ss : TStringStream; begin ss := TStringStream.)); { 指定gb2312的中文代码页,或者54936(gb18030)更好些 utf8 对 ...

  4. ListCell Animation in ListView

    After a long time I am back again with new stuffs. I have seen that JavaFX has got so many demand no ...

  5. poj 2594 Treasure Exploration(最小路径覆盖+闭包传递)

    http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total ...

  6. Umbraco中更换IndexSet中的NodeType后,搜索页面没有做出对应更改的效果

    在项目开发中,使用ExternalSearcher,有一个ExamineIndex.config文件中存放ExternalIndexSet 开始时是这样的 <!-- Default Indexs ...

  7. makefile中一些符号的含义

    关于gnu make的详细介绍参看http://www.gnu.org/software/make/manual/make.html   规则 让我们先来粗略地看一看Makefile的规则. targ ...

  8. WEB数据挖掘(十三)——关联规则与序列模式(1)

    一.Apriori算法简介:  Apriori算法是一种挖掘关联规则的频繁项集算法,其核心思想是通过候选集生成和情节的向下封闭检测两个阶段来挖掘频繁项集. Apriori(先验的,推测的)算法应用广泛 ...

  9. hdoj 5386 Cover

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 倒着推的一个挺暴力的题,看着和数学有关系,然而并没有, 不要一看到含有数学元素就考虑这是一个数学 ...

  10. Mahalanobis Distance(马氏距离)

    (from:http://en.wikipedia.org/wiki/Mahalanobis_distance) Mahalanobis distance In statistics, Mahalan ...