Array.h
资源名称:CAD.zip [点击查看]
上传用户:hehe2haha
上传日期:2013-08-16
资源大小:161k
文件大小:2k
源码类别:
CAD
开发平台:
Visual C++
- // Array.h: interface for the CArray class.
- //
- //////////////////////////////////////////////////////////////////////
- #if !defined(AFX_ARRAY_H__D06D4410_2BCF_4F67_8852_05EF6312A10B__INCLUDED_)
- #define AFX_ARRAY_H__D06D4410_2BCF_4F67_8852_05EF6312A10B__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- template <class T>
- class CArray
- {
- public:
- CArray()
- {
- size = 100;
- top = -1;
- element = new T[size];
- }
- virtual ~CArray()
- {
- RemoveAll();
- delete []element;
- }
- public:
- //增加一个元素
- void Add( T nT)
- {
- if(top+1==size)
- GrowArray(100);
- element[++top] = nT;
- }
- //数组动态增长
- void GrowArray(int nLength)
- {
- T *pTempElement = new T[size +nLength];
- for(int i= 0;i<size;i++)
- {
- pTempElement[i] = element[i];
- }
- delete []element;
- size += nLength;
- element = pTempElement;
- }
- //删除一个元素
- BOOL RemoveAt(int nIndex)
- {
- if(nIndex<0||nIndex>top)
- return 0;
- else
- {
- if(element[nIndex] != NULL)
- {
- delete element[nIndex];
- }
- for(int i = nIndex+1;i<=top;i++)
- {
- element[i-1] = element[i];
- }
- top--;
- return 1;
- }
- }
- //删除全部元素
- void RemoveAll()
- {
- for(int i = 0;i<=top;i++)
- {
- if(element[i] != NULL)
- {
- delete element[i];
- element[i]= NULL;
- }
- }
- top = -1;
- }
- T operator[](int nPos)
- {
- if(nPos<0||nPos>top)
- throw "error";
- return element[nPos];
- }
- int GetArraySize()
- {
- return (top+1);
- }
- public:
- T *element;
- int size;
- int top;
- };
- #endif // !defined(AFX_ARRAY_H__D06D4410_2BCF_4F67_8852_05EF6312A10B__INCLUDED_)