优先级队列,代码参考范例

一个看起来比较规范的代码:

1、版本信息

2、预处理信息

3、库函数引用

4、泛型编程

5、宏定义

6、复制构造函数

7、内敛函数

8、变量命名规范

9、代码的时间空间效率

10、错误恢复能力

11、规范的注释和缩进

代码范例:

 

/***************************************** Project:PriorityQueue** File:priqueue.h** Edition:v1.0.0 Demo** Coder:KingsamChen [MDSA Group]** Last Modify:2011-9-1****************************************/#if _MSC_VER > 1000
#pragma once
#endif#ifndef _PRIQUEUE_05232021_A052_400f_ADD6_8FA21FDCBF7E
#define _PRIQUEUE_05232021_A052_400f_ADD6_8FA21FDCBF7E#include <cassert>
#include <cstdio>#define PARENT(x) (((x) - 1) >> 1)
#define LEFTCHILD(x) (((x) << 1) + 1)
#define RIGHTCHILD(x) (((x) + 1) << 1)template<typename T>
class CPriQueue
{public:typedef int pos;public:CPriQueue();CPriQueue(const CPriQueue& q);~CPriQueue();public:CPriQueue& operator =(const CPriQueue& q);void BuildHeap(const T ary[], int count);void Insert(const T& ele);T ExtractMin();inline T Min() const;inline int GetCount() const;inline bool IsEmpty() const;inline bool IsFull() const;pos Find(const T& ele) const;void DecreaseKey(pos p, unsigned int det);void IncreaseKey(pos p, unsigned int det);void Delete(pos p);// diagnostic interface#if _DEBUGvoid DgPrint();#endifprivate:void PercolateUp(int i, const T& ele);void PercolateDown(int i, const T& ele);private:enum{INI_CAPCITY = 50, NOT_FOUND = -1};T* m_pHeap;int m_capcity;int m_count;
};template<typename T>
CPriQueue<T>::CPriQueue() : m_count(0)
{m_pHeap = new T[INI_CAPCITY];assert(m_pHeap != NULL);m_capcity = INI_CAPCITY;
}template<typename T>
CPriQueue<T>::CPriQueue(const CPriQueue& q) : m_capcity(q.m_capcity),m_count(q.m_count)
{m_pHeap = new T[m_capcity];assert(m_pHeap != NULL);	// the element may have internal handle pointing to the extra data outside// assume that the object already overloaded operator =for (int i = 0; i < m_count; ++i){m_pHeap[i] = q.m_pHeap[i];}
}template<typename T>
CPriQueue<T>::~CPriQueue()
{if (m_pHeap != NULL){delete [] m_pHeap;m_pHeap = NULL;m_capcity = 0;m_count = 0;}
}template<typename T>
CPriQueue<T>& CPriQueue<T>::operator =(const CPriQueue& q)
{if (m_capcity < q.m_count){// need to expandassert(false);}m_count = q.m_countfor (int i = 0; i < m_count; ++i){m_pHeap[i] = q.m_pHeap[i];}	return *this;
}template<typename T>
void CPriQueue<T>::Insert(const T& ele)
{if (IsFull()){// Logs error or expands capcity of the heapassert(false);}// new element may violate heap propertyPercolateUp(m_count, ele);++m_count;
}/*Description:Adjusts the specific element which may violate the heap propertyupward.Parameters:i[in] - the position in the heap of the specific element. ele[in] - a copy of the element. It's used to make the function moreefficient. Do not have this parameter refered to the element directly.It may possible change the value of the ele while adjusting.Return Value:none
*/
template<typename T>
void CPriQueue<T>::PercolateUp(int i, const T& ele)
{for (int p = PARENT(i); ele < m_pHeap[p]; p = PARENT(p)){// reaches the rootif (0 == i){break;}m_pHeap[i] = m_pHeap[p];i = p;}m_pHeap[i] = ele;
}template<typename T>
T CPriQueue<T>::ExtractMin()
{assert(!IsEmpty());T ret(m_pHeap[0]);// new root violates the heap propertyPercolateDown(0, m_pHeap[--m_count]);return ret;
}/*Description:It is Similar to the function PercolateUp but downward.Parameters:i[in] - the position in the heap of the specific element. ele[in] - the same as in PercolateUpReturn Value:none
*/
template<typename T>
void CPriQueue<T>::PercolateDown(int i, const T& ele)
{for (; LEFTCHILD(i) < m_count;){// the node may have only left childint iL = LEFTCHILD(i);int iR = RIGHTCHILD(i);int iMin = iR < m_count ? (m_pHeap[iL] < m_pHeap[iR] ? iL : iR) : iL;		if (m_pHeap[iMin] < ele){m_pHeap[i] = m_pHeap[iMin];i = iMin;} else{break;}}m_pHeap[i] = ele;
}template<typename T>
inline T CPriQueue<T>::Min() const
{assert(!IsEmpty());return m_pHeap[0];	
}template<typename T>
inline int CPriQueue<T>::GetCount() const
{return m_count;
}template<typename T>
inline bool CPriQueue<T>::IsEmpty() const
{return 0 == m_count ? true : false;
}template<typename T>
inline bool CPriQueue<T>::IsFull() const
{return m_capcity == m_count ? true : false;
}/*Description:Returns the position of the specific element to be found. The functiontakes O(N) timeParameters:ele[in] - the element we search forReturn Value:The function returns NOT_FOUND if the specific element is not foundotherwise the return value indicates the position of the element
*/
template<typename T>
typename CPriQueue<T>::pos CPriQueue<T>::Find(const T& ele) const
{pos index = NOT_FOUND;for (int i = 0; i < m_count; ++i){if (m_pHeap[i] == ele){index = i;break;}}return index;
}template<typename T>
void CPriQueue<T>::DecreaseKey(pos p, unsigned int det)
{assert(p >= 0);m_pHeap[p] -= det;T newEle(m_pHeap[p]);// adjusts the order propertyPercolateUp(p, newEle);
}template<typename T>
void CPriQueue<T>::IncreaseKey(pos p, unsigned int det)
{assert(p >= 0);m_pHeap[p] += det;T newEle(m_pHeap[p]);PercolateDown(p, newEle);	
}template<typename T>
void CPriQueue<T>::Delete(pos p)
{assert(p >= 0);int det = m_pHeap[p] - m_pHeap[0] + 1;DecreaseKey(p, det);ExtractMin();
}/*Description:Builds up the heap from an arrayParameters:ary[in] - the array contains elementscount[in] - indicates the counts of the elements in arrayReturn Value:none
*/
template<typename T>
void CPriQueue<T>::BuildHeap(const T ary[], int count)
{assert(m_capcity >= count);for (int i = 0; i < count; ++i){m_pHeap[i] = ary[i];}m_count = count;for (int i = PARENT(count - 1); i >= 0; --i){T eleMov(m_pHeap[i]);PercolateDown(i, eleMov);}
}#if _DEBUG
template<typename T>
void CPriQueue<T>::DgPrint()
{for (int i = 0; i < m_count; ++i){wprintf_s(L"%d\t", m_pHeap[i]);}wprintf_s(L"\n");
}
#endif#endif

  

转载于:https://www.cnblogs.com/fistao/archive/2013/05/29/3106154.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/261617.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

r数据框计算字符出现次数_R语言系列第二期:①R变量、脚本、作图等模块介绍...

在上一篇文章里&#xff0c;给大家介绍了R语言的下载&#xff0c;界面操作&#xff0c;6个处理对象等等。在这些内容的基础上&#xff0c;我们在这个部分为大家介绍一些实用知识&#xff0c;包括描述工作区结构、图形设备以及它们的参数等问题&#xff0c;还有初级编程和数据输…

$.AjaxFileUpload is not a function

2019独角兽企业重金招聘Python工程师标准>>> ..is not a function错误的可能情况&#xff1a; 1、JS引入的路径不对。检查方法是看浏览器控制台是否将JS载入了进来。 2、JS引入顺序不对。JS要在你使用之前引入 3、Jquery没有第一个引入。 4、函数所在script标签&…

打印一个整数二进制表示中所有的奇数位和偶数位

#include<stdio.h>void my_print(int n){int i 0;printf(" 打印奇数位\n");for (i 30; i >0; i i - 2){printf("%d ", (n >> i)&1);}printf("\n");printf("打印偶数位\n");for (i 31; i >1; i i - 2){prin…

60个数据窗口技巧(转)

60个数据窗口技巧 1.如何让存储文件目录的列&#xff0c;显示图片? 答&#xff1a;选择对应的column的display as picture属性为true 2、如何复制grid类型的所选择的行的数据到系统剪切板&#xff1f;答&#xff1a;string ls_selectedls_selecteddw_1.Object.DataWindow.Sel…

代理对象我所理解的设计模式(C++实现)——代理模式(Proxy Pattern)

文章结束给大家来个程序员笑话&#xff1a;[M] 概述 作为C工程师&#xff0c;免不了要管理内存&#xff0c;内存管理也是C中的难点&#xff0c;而智能指针采用引用计数的方法很方便的帮我们管理了内存的应用&#xff0c;极大方便了我们的任务效率。而智能指针的这类用法其实就是…

suse 内核编译安装_升级SUSE Linux内核的完整步骤!

安装完SLED 10后发现仍然有“热启动网络不通”的问题&#xff0c;原因是内核版本较低&#xff0c;于是升级到2.6.17版内核&#xff0c;成功解决此问题。为了造福广大菜鸟&#xff0c;分享一下我的经验&#xff0c;欢迎高手批评补充。1。将下载的新内核(比如linux-2.6.17.tar.bz…

C语言逆序字符串(递归实现)

算法思想 以字符串“abcdef”为例 1.将a保存到temp中 2.将f放到a的位置 3.f处填写‘\0’ 4.计算bcde的长度 5.如果bcde的长度大于1&#xff0c;则逆序 6.将a放到f的位置 #include <stdio.h> #include<string.h> int my_strlen(char* str) {char* start str;char*…

android 自定义控件

自定义一般分三种情况 1. 自定义布局 2. 自定义控件 3.直接继承View 下面来着eoe例子&#xff0c;实现自定义控件 1. 自定义属性 res/values/attrs.xml 自定义属性 <?xml version"1.0" encoding"utf-8"?> <resources><declare-styleable …

某项目要调用现有的100多个DLL 一 开始

某个项目需要使用很多内部其他Team的DLL 和第三方的DLL (大概百来个吧......这是什么鬼啊...可怕的历史,又不能改 这些dll都上生产环境了) 如果直接用dll引用之后调用 会有很多很多的问题 1.程序的质量会下降到 这百来个dll中质量最差的一个 (万一某些人在代码里面写什么lock(…

Hibernate3 jar包的作用[转]

from:http://nopainnogain.iteye.com/blog/761630 &#xff08;1&#xff09;hibernate3.jar: Hibernate的核心库&#xff0c;没有什么可说的&#xff0c;必须使用的jar包 &#xff08;2&#xff09;cglib-asm.jar: CGLIB库&#xff0c;Hibernate用它来实现PO字节码的动态生成&…

Linux下mysql整库备份

基本语法&#xff1a;mysqldump -u[mysql账户名] -p [--default-character-setutf8] [--max_allowed_packet50M]数据库名 >文件保存路径.示例&#xff1a; mysqldump -uroot -p --default-character-setutf8 --max_allowed_packet50M hyman > /web/hyman.sql转载于:htt…

svr公式推导_支持向量回归(SVR)的详细介绍以及推导算法

1 SVR背景2 SVR原理3 SVR数学模型SVR的背景SVR做为SVM的分支从而被提出&#xff0c;一张图介绍SVR与SVM的关系这里两虚线之间的几何间隔rd ∣ ∣ W ∣ ∣ \frac{d}{||W||}∣∣W∣∣d​,这里的d就为两虚线之间的函数间隔。(一图读懂函数间隔与几何间隔)这里的r就是根据两平行线之…

计算整数中各位数字之和

#include<stdio.h> int digit_sum(int num) {int temp num % 10;if (num > 9)return temp digit_sum(num / 10);elsereturn temp; } int main() {int num 0;scanf("%d", &num);int sumdigit_sum(num);printf("%d\n", sum);return 0; }

Office文档模型深入---Outlook文档模型与开发实战(1)

简介 本篇为Office文档模型深入系列第4篇&#xff0c;原计划是Excel的图表&#xff0c;之后是Word&#xff0c;因为项目转手需要总结Outlook&#xff0c;先改变下顺序&#xff0c;后面的内容会慢慢补上。本篇为Outlook子系列的第一篇&#xff0c;主要介绍下outlook命名空间下…

VS2003,VS2005,VS2008 低版本打开高版本的解决方案和工程文件

一、用记事本打开sln文件&#xff0c;将&#xff1a; Microsoft Visual Studio Solution File, Format Version 10.00 # Visual Studio 2008 改成&#xff1a; Microsoft Visual Studio Solution File, Format Version 9.00 # Visual Studio 2005 二、用记事本打开csproj文件&a…

写一个js向左滑动删除 交互特效的插件——Html5 touchmove

需求描述 需要实现类似QQ中对联系人的操作&#xff1a;向左滑动&#xff0c;滑出删除按钮。滑动超过一半时松开则自动滑到底&#xff0c;不到一半时松开则返回原处。 纯js实现 使用了h5的touchmove等事件&#xff0c;以及用js动态改变css3的translate属性来达到动画效果&#x…

指数高通滤波器代码_高通滤波法、微分算子法、神经网络方法实现图像边缘检测...

边缘检测(Edge detection)是图像处理和计算机视觉中的基本问题&#xff0c;边缘检测的目的是标识数字图像中亮度变化明显的点。本文使用多种不同的方法&#xff0c;实现对 Lena 肖像的边缘检测&#xff0c;研究分析各算法的效果和优缺点。所涉及的方法如下&#xff1a;高通滤波…

Extjs prompt 显示密码框

Extjs 的 prompt 默认是普通的输入框&#xff0c;我们可以通过获取里面的元素&#xff0c;自己改一下。。呵呵 Ext.Msg.prompt("密码","请输入密码:",function(btn,text){if(btn "ok"){Ext.Msg.alert("ok...","验证密码。。。&q…

Android----Fragments详解

Fragments 概念是在Android3.0版本就已经有了&#xff0c;3.0版本是Tab(平板)专用&#xff0c;后来在4.0以上的版本继续沿 用Fragments&#xff0c;改善了Activity的灵活性。 在没有Fragments之前&#xff0c;一个屏幕就只能放一个Activity&#xff0c;有了Fragments之后&#…

matlab中find()函数用法

一.基本用法 返回矩阵或向量中非零元素的索引 注意&#xff1a;matlab中下标从1开始 举例&#xff1a; &#xff08;1&#xff09;向量 返回非零元素下标 find&#xff08;vector&#xff09; x[1 2 3 0 0 6 7 8 9]; find(x)ans 1 2 3 6 7 8 9返回前…