数组

  1 // MyArray.h: interface for the MyArray class.
  2 //
  3 //////////////////////////////////////////////////////////////////////
  4 
  5 #if !defined(AFX_MYARRAY_H__C353685F_371A_47F8_A9FA_79B110474B9C__INCLUDED_)
  6 #define AFX_MYARRAY_H__C353685F_371A_47F8_A9FA_79B110474B9C__INCLUDED_
  7 
  8 #if _MSC_VER > 1000
  9 #pragma once
 10 #endif // _MSC_VER > 1000
 11 
 12 #include <iostream.h>
 13 
 14 template<typename T>
 15 class MyArray
 16 {
 17 private:
 18     T *m_pData;
 19     int m_nSpace;
 20     int m_nCount;
 21 
 22 public:
 23     MyArray()
 24     {
 25         m_pData = NULL;
 26         m_nSpace = m_nCount = 0;
 27     }
 28 
 29     virtual ~MyArray()
 30     {
 31         delete[] m_pData;
 32         m_pData = NULL;
 33     }
 34 
 35 public:
 36     /************************************************************************/
 37     /* inline                                                               */
 38     /************************************************************************/
 39     //是否为空
 40     bool isEmpty() const
 41     {
 42         return m_nCount == 0;
 43     }
 44 
 45     //获取长度
 46     int GetCount() const
 47     {
 48         return m_nCount;
 49     }
 50 
 51     //获取元素
 52     T GetElem(int nIndex) const
 53     {
 54         return m_pData[nIndex];
 55     }
 56 
 57     //检查范围
 58     bool CheckRange(int nIndex) const
 59     {
 60         if (nIndex <|| nIndex >= m_nCount)
 61         {
 62             throw "索引越界";
 63         }
 64         return true;
 65     }
 66 
 67 public:
 68     /************************************************************************/
 69     /* extern function                                                      */
 70     /************************************************************************/
 71     //检查空间
 72     bool CheckSpace();
 73 
 74     //在尾部增加元素
 75     void AddTailElem(const T& Object);
 76 
 77     //在索引后插入元素
 78     bool InsertElem(int nIndex, const T& Object);
 79 
 80     //删除元素(按索引值删除)
 81     void DeleteElem(int nIndex);
 82 
 83     //查找元素(找到后返回索引值,未找到返回-1)
 84     int FindElem(const T& Object);
 85 
 86     //打印
 87     void Print();
 88 };
 89 
 90 template<typename T>
 91 bool MyArray<T>::CheckSpace()
 92 {
 93     if (m_nSpace == m_nCount)
 94     {
 95         T *pNewSpace = new T[m_nSpace = m_nSpace ? m_nSpace*2 : 1];
 96         for (int i = 0; i < m_nCount; i++)
 97         {
 98             pNewSpace[i] = m_pData[i];
 99         }
100         
101         delete[] m_pData;
102         m_pData = pNewSpace;
103     }
104 
105     return true;
106 }
107 
108 template<typename T>
109 void MyArray<T>::AddTailElem(const T& Object)
110 {
111     CheckSpace();
112     m_pData[m_nCount++] = Object;
113 }
114 
115 template<typename T>
116 bool MyArray<T>::InsertElem(int nIndex, const T& Object)
117 {
118     CheckRange(nIndex);
119     CheckSpace();
120 
121     for (int i = nIndex + 1; i < m_nCount; i++)
122     {
123         m_pData[i + 1] = m_pData[i];
124     }
125     m_pData[nIndex + 1] = Object;
126     m_nCount++;
127 
128     return true;
129 }
130 
131 template<typename T>
132 void MyArray<T>::DeleteElem(int nIndex)
133 {
134     CheckRange(nIndex);
135     
136     for (int i = nIndex; i < m_nCount; i++)
137     {
138         m_pData[i] = m_pData[i + 1];
139     }
140     m_nCount--;
141 }
142 
143 template<typename T>
144 int MyArray<T>::FindElem(const T& Object)
145 {
146     for (int i = 0; i < m_nCount; i++)
147     {
148         if (m_pData[i] == Object)
149         {
150             return i;
151         }
152     }
153     
154     return -1;
155 }
156 
157 template<typename T>
158 void MyArray<T>::Print()
159 {
160     for (int i = 0; i < m_nCount; i++)
161     {
162         cout << m_pData[i] << "\t" << flush; 
163     }
164     cout << endl;
165 }
166 
167 #endif // !defined(AFX_MYARRAY_H__C353685F_371A_47F8_A9FA_79B110474B9C__INCLUDED_)

 

posted @ 2012-11-29 16:24  dreamasm  阅读(115)  评论(0编辑  收藏  举报