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

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

// 优先级队列.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. Chapter6:函数

    执行函数的第一步是(隐式地)定义并初始化它的形参.所以,函数最外层作用域中的局部变量也不能使用与函数形参一样的名字. 局部静态变量:在程序的执行路径第一次经过对象定义语句时初始化,并且直到程序终止才被 ...

  2. Embedded之Introduction

    1 Embedded system An embedded system is a combination of computer hardware and software, and perhaps ...

  3. Java学习日志-01-Hello World

    1.安装JDK1.7 2.安装eclipse 3.eclipse上写第一个java程序-hello world 先建工程,再建包,养成良好的习惯,然后新建类 若不先建立包,可能会提示"The ...

  4. 桶排序-C-结构体排序

    struct TS { int index; ]; }; ] = {{,,,,,"s8"}}; ]; int i; int length = sizeof(a) / sizeof ...

  5. [WinForm] 使用 WebBrowser 操作 HTML 頁面的 Element-摘自网络

    前言 在 Window Form 應用程式如果需要瀏覽網頁時可以崁入 WebBrowser 控制項,但如果需要操作崁入的 HTML 的網頁元素,就需要額外的操作,以下紀錄幾種操作 HTML 元素的方法 ...

  6. 数据库中使用 Synonym和openquery

    如果,你想在一台数据库服务器上,查询另一个台数据服务器的数据该如何做呢?如果,你想在同一台数据服务器上,在不同的数据库之间查询数据,又该怎么办呢?那就让我为你介绍Synonym和openquery吧. ...

  7. 第二百二十二天 how can I 坚持

    纪念碑谷好费脑子啊,头都大了,被遗忘的海最后百度了下攻略才过了. 今天下班遇到了易军,哎,总感觉怪怪的,心情顿时压抑了些,源二生日,一起去吃了个饭,烤鸭,吃的挺不错. 创新去哪了,其实每个人的内心深处 ...

  8. Javascript函数柯里化(curry)

    函数柯里化currying,是函数式编程非常重要的一个标志.它的实现需要满足以下条件,首先就是函数可以作为参数进行传递,然后就是函数可以作为返回值return出去.我们依靠这个特性编写很多优雅酷炫的代 ...

  9. RS-232-C串口通讯协议解析(硬件接口协议)

    http://www.dz3w.com/info/interface/0075524.html http://wenku.baidu.com/view/02cc247c27284b73f24250e3 ...

  10. RGPJS 教程之八 创造场景

    开始画面 游戏画面 代码 <!DOCTYPE html> <html> <head> <script src="rpg-beta-2.js" ...