vc++简单的vector动态数组实现
1 #ifndef __MYVECTOR__ 2 #define __MYVECTOR__ 3 #include <Windows.h> 4 #define SUCCESS 1 // 成功 5 #define ERRORS -1 // 失败 6 #define MALLOC_ERROR -2 // 申请内存失败 7 #define INDEX_ERROR -3 // 错误的索引号 8 template<typename TELE> 9 class Vector 10 { 11 public: 12 Vector(void); 13 Vector(DWORD dwSize); 14 ~Vector(); 15 public: 16 DWORD at(DWORD dwIndex,OUT TELE* pEle); //根据给定的索引得到元素 17 DWORD push_back(TELE Element); //将元素存储到容器最后一个位置 18 VOID pop_back(); //删除最后一个元素 19 DWORD insert(DWORD dwIndex, TELE Element); //向指定位置新增一个元素 20 DWORD capacity(); //返回在不增容的情况下,还能存储多少元素 21 VOID clear(); //清空所有元素 22 BOOL empty(); //判断Vector是否为空 返回true时为空 23 DWORD erase(DWORD dwIndex); //删除指定元素 24 DWORD size(); //返回Vector元素数量的大小 25 private: 26 BOOL expand(); //容器满了需要扩充 27 private: 28 DWORD m_dwIndex; //下一个可用索引 29 DWORD m_dwIncrement; //每次增容的大小 30 DWORD m_dwLen; //当前容器可用的长度 31 DWORD m_dwInitSize; //默认初始化大小 32 TELE *m_pVector; //容器指针 33 34 }; 35 template<typename TELE> 36 Vector<TELE>::Vector(void) //无参构造函数 37 :m_dwInitSize(100), 38 m_dwIncrement(5), 39 m_dwIndex(0) 40 { 41 m_pVector = new TELE[m_dwInitSize]; 42 ZeroMemory(m_pVector,sizeof(TELE)*m_dwInitSize); 43 m_dwLen = m_dwInitSize; 44 } 45 template<typename TELE> 46 Vector<TELE>::Vector(DWORD dwSize) //有参构造函数 47 :m_dwInitSize(dwSize), 48 m_dwIndex(0), 49 m_dwIncrement(5) 50 { 51 m_pVector = new TELE[m_dwInitSize]; 52 ZeroMemory(m_pVector,sizeof(TELE)*m_dwInitSize); 53 m_dwLen = m_dwInitSize; 54 } 55 template<typename TELE> 56 Vector<TELE>::~Vector() //析构函数 57 { 58 if(m_pVector)delete [] m_pVector; 59 m_dwLen=NULL; 60 m_dwIncrement = NULL; 61 m_dwInitSize = NULL; 62 m_pVector = NULL; 63 m_dwIndex = NULL; 64 } 65 template<typename TELE> 66 DWORD Vector<TELE>::at(DWORD dwIndex,OUT TELE* pEle) //根据给定的索引得到元素 67 { 68 if(empty()||dwIndex >= m_dwIndex) 69 return INDEX_ERROR; 70 else 71 { 72 CopyMemory(pEle,&m_pVector[dwIndex],sizeof(TELE)); 73 } 74 return SUCCESS; 75 } 76 template<typename TELE> 77 BOOL Vector<TELE>::expand() //容器满了需要扩充 78 { 79 //先申请内存空间用来存储新的数据 80 TELE* m_TempPtr = nullptr; 81 DWORD dw_NewInitSize = m_dwInitSize + m_dwIncrement; 82 m_TempPtr = new TELE[dw_NewInitSize]; 83 if (!m_TempPtr)return false; 84 ZeroMemory(m_TempPtr,sizeof(TELE)*dw_NewInitSize); 85 CopyMemory(m_TempPtr,m_pVector,sizeof(TELE)*m_dwInitSize); 86 m_dwInitSize = dw_NewInitSize; 87 delete [] m_pVector; //释放原来的指针 88 m_pVector = m_TempPtr; //把新的指针给头指针 89 m_dwLen += m_dwIncrement; 90 return true; 91 } 92 template<typename TELE> 93 DWORD Vector<TELE>::push_back(TELE Element) //将元素存储到容器最后一个位置 94 { 95 //先判断容器是否满了 96 if(m_dwLen <= 0 || m_dwIndex >= m_dwInitSize) 97 { 98 //需要扩充 99 if(!expand()) 100 { 101 return ERRORS; 102 } 103 } 104 CopyMemory(&m_pVector[m_dwIndex],&Element,sizeof(TELE)); 105 m_dwIndex++; 106 m_dwLen--; 107 return SUCCESS; 108 } 109 template<typename TELE> 110 VOID Vector<TELE>::pop_back() //删除最后一个元素 111 { 112 //先确定它是不是空的 113 if (empty())return; 114 ZeroMemory(&m_pVector[m_dwIndex-1],sizeof(TELE)); 115 m_dwIndex--; 116 m_dwLen++; 117 /*return SUCCESS;*/ 118 } 119 template<typename TELE> 120 DWORD Vector<TELE>::insert(DWORD dwIndex, TELE Element) //向指定位置新增一个元素 121 { 122 //先判断给的索引是否可以插入 123 if (dwIndex < 0 || dwIndex > m_dwIndex) 124 { 125 return INDEX_ERROR; 126 } 127 if(dwIndex == m_dwIndex) 128 { 129 CopyMemory(&m_pVector[m_dwIndex],&Element,sizeof(TELE)); 130 return SUCCESS; 131 } 132 if(m_dwLen <=0 || m_dwIndex >= m_dwInitSize) //判断是否够插入新元素 133 { //如果不够就要扩充 134 if(!expand())return ERRORS; 135 } 136 //0 1 2 3 4 5 6 7 137 //把要插入的索引的位置后面的元素后移 138 for(DWORD i = m_dwIndex;i>dwIndex;i--) 139 CopyMemory(&m_pVector[i],&m_pVector[i-1],sizeof(TELE)); 140 CopyMemory(&m_pVector[dwIndex],&Element,sizeof(TELE)); 141 m_dwIndex++; 142 m_dwLen--; 143 return SUCCESS; 144 } 145 template<typename TELE> 146 DWORD Vector<TELE>::capacity() //返回在不增容的情况下,还能存储多少元素 147 { 148 return m_dwLen; 149 } 150 template<typename TELE> 151 VOID Vector<TELE>::clear() //清空所有元素 152 { 153 if(empty())return; 154 for (DWORD i =0;i<m_dwIndex;i++) 155 ZeroMemory(&m_pVector[i],sizeof(TELE)); 156 m_dwLen = NULL; 157 m_dwIndex = NULL; 158 m_dwInitSize = NULL; 159 m_dwIncrement = NULL; 160 } 161 template<typename TELE> 162 BOOL Vector<TELE>::empty() //判断Vector是否为空 返回true时为空 163 { 164 if(m_dwIndex) 165 return false; 166 else 167 return true; 168 } 169 template<typename TELE> 170 DWORD Vector<TELE>::erase(DWORD dwIndex) //删除指定元素 171 { 172 if(empty() || dwIndex < 0)return INDEX_ERROR; // 173 ZeroMemory(&m_pVector[dwIndex],sizeof(TELE)); 174 for (DWORD i =dwIndex;i<m_dwIndex;i++) //把指定索引后面的元素向前移动即可 175 CopyMemory(&m_pVector[i],&m_pVector[i+1],sizeof(TELE)); 176 m_dwLen++; 177 m_dwIndex--; 178 return SUCCESS; 179 } 180 template<typename TELE> 181 DWORD Vector<TELE>::size() //返回Vector元素数量的数量 182 { 183 return (m_dwIndex-1); 184 } 185 186 #endif