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

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

// 优先级队列.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. Asp.Net学习进度备忘(第一步:ASP.NET Web Forms)

    书签:“Web Pages”和“MVC”跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1.ASP. ...

  2. Intent相关

    Intent是什么? 翻译为:意图,目的(名词) 其实根本没必要管它是什么,看看它能做什么就好了. 不过后来我知道了,它就是个机制----通信机制-----android的许多组件间的交流要依赖它. ...

  3. PHP 代码质量检测工具的安装与使用

    代码统计工具 PHPLOC安装:wget https://phar.phpunit.de/phploc.phar chmod +x phploc.phar sudo mv phploc.phar /u ...

  4. 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路

    1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...

  5. Research on Unsupervised Word Alignment Based on Inversion Transduction Grammar

    1.提出了一种基于特征函数和反向转录文法(ITG)的无监督词对齐模型,使用对数线性模型对文法规则的概率建模,先验知识可以通过特征函数的形式加入到模型里面,而模型仍然可以进行无监督训练.2. 在模型的参 ...

  6. 基于MapReduce的关系代数运算(1)

    1.选择运算 Map函数:对R中的每个元组t,检测它是否满足条件C,如果满足,则产生一个键值对(t,t) Reduce函数:直接将每个键值对传递到输出即可 2.投影运算 Map函数:对R中的每个元组t ...

  7. mediawiki 的使用

    首先,程序里会先加载 includes/DefaultSettings.php,然后再加载 LocalSettings.php,这样定义一些权限.其中 DefaultSettings.php 是默认的 ...

  8. shell's glob

    [shell's glob] basic glob example: range glob example: 参考: http://bash.cumulonim.biz/glob.html

  9. centos下安装mysql不能启动

    初学者犯了个错误:yum安装mysql的命令是:yum -y install mysql-server,而不是yum -y install mysql ----------------------以下 ...

  10. socket的一个错误的解释SocketException以及其他几个常见异常

    写socket程序有可能会遇见这个问题  其他信息: 由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受.  这种情况我的 错误原因 ...