動態數組vector
阿新 • • 發佈:2017-09-18
++ end 數組長度 std template cout main 賦值 實現
動態數組
動態數組可以實現長度的自由變化,但是通過vector的默認值可以基本判斷所謂動態數組實際上還是一個普通數組,傳遞一個參數確定數組長度,如果沒有傳遞參數,程序中其實默認進行設定。如果插入數據之後超過了已有長度,則在內部進行了一個創建第三方的過程,將現在的數組保存起來,然後再次創建一個新的長度的數組,然後根據插入值進行重新整合。
筆者並未看過源碼,只是按照功能試著進行實現。
1.array.h
1 #pragma once 2 3 #include <iostream> 4 using namespace std; 5 6 78 template <typename T> 9 class Array 10 { 11 private: 12 T * Arr; //動態數組首地址 13 int iRail_Pos; //動態數組最後元素位置 14 int IsBlank; //判斷是否為空,0為空,1為非空 15 int Len ; //數組長度 16 public: 17 Array(); //初始化動態數組,不初始化元素個數,是一個空數組 18 Array(int n); //初始化動態數組,元素個數為n 19 voidPush_Back(T val); //在尾部添加元素 20 void Pop_Back(); //在尾部刪除元素 21 void Show(); //顯示數組 22 bool IsEmpty(); //判斷是否為空 23 int Insert(int pos, T elem); //在pos插入元素elem, 並返回pos 24 int Delete(int pos); //在pos刪除元素elem,並返回pos 25 T At(int idx); //通過下標idx索引數組中的元素 26 }; 27 28 29 30 3132 33 //構造函數,自己設定數組長度 34 template <typename T> 35 Array<T>::Array(int n) 36 { 37 Len = n; 38 Arr = new T [Len]; 39 IsBlank = 0; //0為空,1為非空 40 iRail_Pos = 0; 41 } 42 43 44 45 46 47 //構造函數,默認長度為3 48 template <typename T> 49 Array<T>::Array() 50 { 51 Len = 3; 52 Arr = new T[Len]; //默認為3 53 IsBlank = 0; //0為空,1為非空 54 iRail_Pos = 0; 55 } 56 57 58 59 60 //在尾部添加元素 61 template <typename T> 62 void Array<T>::Push_Back(T val) //給動態數組賦值,只能按順序從尾部依次賦值 63 { 64 if (iRail_Pos < Len) //如果Push_Back元素小於Len,則直接賦值val給Arr[iRail_Pos] 65 { 66 Arr[iRail_Pos] = val; 67 iRail_Pos++; 68 IsBlank = 1; 69 } 70 else 71 { 72 T * Copy_Array = new T[iRail_Pos]; //如果Push_Back元素超出了範圍Len, 創建一個拷貝數組,用於保存原數組 73 for (int i = 0; i < iRail_Pos; i++) 74 Copy_Array[i] = Arr[i]; 75 delete[] Arr; //釋放Arr 76 Arr = new T[iRail_Pos+1]; //再次創建一個新的數組Arr,長度 + 1 77 for (int i = 0; i < iRail_Pos; i++) 78 Arr[i] = Copy_Array[i]; 79 delete[] Copy_Array; 80 Arr[iRail_Pos] = val; 81 IsBlank = 1; 82 iRail_Pos++; 83 } 84 } 85 86 87 88 89 //在pos插入元素elem, 並返回pos 90 template <typename T> 91 int Array<T>::Insert(int pos, T elem) 92 { 93 //如果插入位置在pos之前 94 if (pos <= iRail_Pos && pos > 0) 95 { 96 int m = pos; 97 int n = pos + 1; 98 T * Copy_Array = new T[iRail_Pos]; //如果Push_Back元素超出了範圍Len, 創建一個拷貝數組,用於保存原數組 99 for (int i = 0; i < iRail_Pos; i++) 100 Copy_Array[i] = Arr[i]; 101 delete[] Arr; //釋放Arr 102 Arr = new T[iRail_Pos + 1]; //再次創建一個新的數組Arr,長度 + 1 103 for (int i = 0; i < pos; i++) //----拷貝pos前元素 104 Arr[i] = Copy_Array[i]; 105 106 Arr[pos] = elem; //-----將val裝到pos位置 107 108 for (int i = 0; i < iRail_Pos - pos; i++) //-----拷貝pos後元素 109 { 110 Arr[n] = Copy_Array[m]; 111 n++; 112 m++; 113 } 114 iRail_Pos++; 115 delete[] Copy_Array; 116 } 117 118 //如果插入位置在pos之後 119 if (pos > iRail_Pos) 120 { 121 T * Copy_Array = new T[iRail_Pos]; //如果Push_Back元素超出了範圍Len, 創建一個拷貝數組,用於保存原數組 122 for (int i = 0; i < iRail_Pos; i++) 123 Copy_Array[i] = Arr[i]; 124 delete[] Arr; //釋放Arr 125 Arr = new T[pos + 1]; //再次創建一個新的數組Arr,長度 + 1 126 for (int i = 0; i < iRail_Pos; i++) 127 Arr[i] = Copy_Array[i]; 128 Arr[pos - 1] = elem; 129 iRail_Pos = pos; 130 delete[] Copy_Array; 131 } 132 133 if (pos < 0) 134 cout << "請輸入正確pos!" << endl; 135 return pos; 136 } 137 138 //顯示元素 139 template <typename T> 140 void Array<T>::Show() //顯示數組 141 { 142 for (int i = 0; i < iRail_Pos; i++) 143 { 144 cout << Arr[i] << " "; 145 } 146 cout << endl; 147 } 148 149 150 151 152 //判斷是否為空,true為空,false為非空 153 template <typename T> 154 bool Array<T>::IsEmpty() 155 { 156 if (IsBlank == 0) 157 return true; 158 else 159 return false; 160 } 161 162 163 164 165 166 167 168 template <typename T> 169 int Array<T>::Delete(int pos) //在pos刪除元素elem,並返回pos 170 { 171 172 if (IsBlank) //如果非空 173 { 174 int m = pos; 175 int n = pos + 1; 176 T * a = new T[iRail_Pos]; 177 for (int i = 0; i < iRail_Pos; i++) //復制動態數組 178 a[i] = Arr[i]; 179 for (int i = 0; i < iRail_Pos - pos + 1; i++) 180 { 181 Arr[m] = a[n]; 182 n++; 183 m++; 184 } 185 iRail_Pos--; 186 } 187 else 188 cout << "數組為空,不能執行Delete操作!" << endl; 189 190 return pos; 191 } 192 193 template <typename T> 194 void Array<T>::Pop_Back() //在尾部刪除元素 195 { 196 if (IsBlank) //如果非空 197 { 198 Arr[iRail_Pos] = 0; 199 iRail_Pos--; 200 } 201 else 202 cout << "數組為空,不能執行Pop_Back操作!" << endl; 203 } 204 205 206 template <typename T> 207 T Array<T>::At(int idx) //通過下標idx索引數組中的元素 208 { 209 if (IsBlank) //如果非空 210 return Arr[idx]; 211 else 212 cout << "數組為空,不能執行At操作!" << endl; 213 }
2.main.cpp
1 #include "array.h" 2 3 int main() 4 { 5 Array<int> arr; 6 7 if (arr.IsEmpty()) 8 cout << "空" << endl; 9 else 10 cout << "非空" << endl; 11 12 arr.Delete(2); 13 14 arr.Push_Back(1); 15 arr.Push_Back(2); 16 arr.Push_Back(3); 17 arr.Push_Back(4); 18 arr.Push_Back(5); 19 20 arr.Show(); 21 22 arr.Insert(1, 99); 23 arr.Insert(10, 888); 24 25 arr.Show(); 26 27 arr.Insert(-10, 888); 28 arr.Push_Back(11); 29 arr.Delete(2); 30 31 arr.Show(); 32 33 arr.Pop_Back(); 34 35 arr.Show(); 36 37 if (arr.IsEmpty()) 38 cout << "空" << endl; 39 else 40 cout << "非空" << endl; 41 42 cout << "arr.At(7) = " << arr.At(7) << endl; 43 44 45 system("pause"); 46 47 return 0; 48 }
動態數組vector