c++队列类模板的实现
//队列类模板定义 template <class POINT_TYPE,int MAX_POINTS> class VarQueue { public: VarQueue() { m_MaxCount=MAX_POINTS; m_Count=0; } VarQueue(unsigned int MaxCount) { if(MaxCount>MAX_POINTS) m_MaxCount=MAX_POINTS; else m_MaxCount=MaxCount; m_Count=0; } public: //出队处理,先进先出 unsigned int PopPoint(POINT_TYPE *Point); //入队处理,追加在队的末尾 unsigned int AppendPoint(const POINT_TYPE &Point); //取得某结点的值 bool GetPoint(POINT_TYPE *Point,unsigned int Position)const; //取得结点数 unsigned int GetCount()const; //清空队列 void ClearQueue(); private: //队列的最大结点数 unsigned int m_MaxCount; //队列的实际结点数 unsigned int m_Count; //保存结点的数组 POINT_TYPE m_Queue[MAX_POINTS]; }; /////////////////队列元素类定义 class CElement { public: CElement() { fValue=0.0; } CElement(float value) { fValue=value; } CElement& operator =(int value) { fValue=(float)value; return *this; } CElement& operator =(float value) { fValue=value; return *this; } CElement& operator =(CElement elem) { fValue=elem.fValue; return *this; } operator float() { return fValue; } float GetValue()const { return fValue; } private: float fValue; };
#include "stdio.h" #include "Queue.h" //队列类模板的实现 template <class POINT_TYPE,int MAX_POINTS> unsigned int VarQueue<POINT_TYPE,MAX_POINTS>::PopPoint(POINT_TYPE *Point) { unsigned int i=0; if(m_Count>0) { if(NULL!=Point) *Point=m_Queue[0]; for(i=1;i<m_Count;i++) { m_Queue[i-1]=m_Queue[i]; } m_Count--; } return m_Count; } template <class POINT_TYPE,int MAX_POINTS> unsigned int VarQueue<POINT_TYPE,MAX_POINTS>::AppendPoint(const POINT_TYPE &Point) { if(m_Count<m_MaxCount) m_Queue[m_Count++]=Point; return m_Count; } template <class POINT_TYPE,int MAX_POINTS> bool VarQueue<POINT_TYPE, MAX_POINTS>::GetPoint(POINT_TYPE *Point,unsigned int Position)const { if(Position<m_Count && NULL!=Point) { *Point=m_Queue[Position]; return true; } else return false; } template <class POINT_TYPE,int MAX_POINTS> inline unsigned int VarQueue<POINT_TYPE,MAX_POINTS>::GetCount()const { return m_Count; } template <class POINT_TYPE,int MAX_POINTS> void VarQueue<POINT_TYPE,MAX_POINTS>::ClearQueue() { m_Count=0; } int main(int argc, char* argv[]) { /*将模板实例化为最多容纳50个元素的整型队列,类名为VarQueue<int,50>, 由该类实例化的对象名为iQueue */ VarQueue<int,50> iQueue(20); /*将模板实例化为最多容纳50个元素的CElement型队列,类名为VarQueue <CElement,30>, 由该类实例化的对象名为fQueue */ VarQueue<CElement,30> fQueue(10); int ival=0; unsigned count=0;; CElement fval=1.0; for(unsigned int i=0;i<4;i++) iQueue.AppendPoint(i); count=iQueue.GetCount(); for( i=0;i<count;i++) { iQueue.PopPoint(&ival); fQueue.AppendPoint((float)ival); } for( i=0;i<fQueue.GetCount();i++) { fQueue.GetPoint(&fval,i); printf("%4.2f\n",fval.GetValue()); } iQueue.ClearQueue(); fQueue.ClearQueue(); return 0; }
醉过海阔天空,且狂且痴且醉趁年少