C++基礎學習筆記:自定義陣列模板類
阿新 • • 發佈:2019-01-07
//!時間:2017年9月12日(週二)下午 //!內容:陣列模板類 /* 修改:2017年9月13上午 成員方法中delete未正確匹配 改進:2017年9月13晚上 陣列總量改為固定 */ #define _CRTDBG_MAP_ALLOC #include <iostream> #include <string> #include "ArrayTemplate.h" #include "Arr.h" #include <ctime> int main() { using namespace std; { clock_t one = clock(); Arr<int, 1> testOne(1); for (int i = 0; i < 10000; i++) { testOne.Add(5, 1); } clock_t two = clock(); Array<int, 1> testTwo(1); for (int i = 0; i < 10000; i++) { testTwo.Add(5, 1); } clock_t three = clock(); cout << "Add(5, 1)*10000固定大小arr: " << (double)(two - one) / CLOCKS_PER_SEC << endl; cout << "Add(5, 1)*10000不固定Array: " << (double)(three - two) / CLOCKS_PER_SEC << endl<<endl; cout << "測試int:" << endl; cout << "宣告一個長度為5的int型陣列並賦值:"; Arr<int, 5> test; for (int i = 0; i < test.Size(); i++) { test[i] = i; cout << " " << test[i]; } cout << endl << "間隔插入數字6:"; for (int i = 5; i >= 0; i--) { test.Add(6,i); } for (int i = 0; i < test.Size(); i++) { cout << " " << test[i]; } cout << endl << "第一個數字4位於:位" << test.Search(4); test.Delete(9);//刪除位9的4 test.Reset(4, 2);//將位2設定為4 cout << endl << "Reset(4, 2)後4位於:位" << test.Search(4) << endl << endl; cout << "測試char *:" << endl; cout << "宣告一個長度為3的char型陣列並賦值:\n"; Arr<char *, 3> str("aa"); for (int i = 0; i < str.Size(); i++) { cout << str[i]<<" "; } str.Add("add"); str.Reset("reset", 1); cout << "\n現在陣列內容為:\n"; for (int i = 0; i <str.Size(); i++) { cout << str[i]<<" "; } cout << endl<<endl<<"測試string:\n"; Arr<string, 3> ss("string"); for (int i = 3; i >= 0; i--) { ss.Add("aa", i); } ss.Reset("111", 1); for (int i = 0; i <ss.Size(); i++) { cout << ss[i] << endl; } cout << endl; } system("pause"); _CrtDumpMemoryLeaks(); return 0; }
#pragma once template <typename T, int n> class Arr { private: static const int LIMIT = 11000; int count = n; T *arr; public: Arr()//構造 { if (n > LIMIT) throw "丟一個異常"; arr = new T[LIMIT]; } explicit Arr(const T&t)//構造 { if (n > LIMIT) throw "丟一個異常"; arr = new T[LIMIT]; for (int i = 0; i < n; i++) arr[i] = t; } ~Arr()//析構 { delete[]arr; } //下標訪問 T&operator[](int i); //插入or增加一個元素 bool Add(const T&t, int i = -1); //刪除某個元素 bool Delete(int i = -1); //重置某個元素 bool Reset(const T&t, int i); //查詢某個元素第一個下標 int Search(const T&t); //查詢陣列長度 int Size(); }; template <typename T, int n> T& Arr<T, n>::operator[](int i) { if (i<0 || i >= count) { throw "out of limits"; } return arr[i]; } template <typename T, int n> bool Arr<T, n>::Add(const T&t, int i) { if (count + 1 > LIMIT) { return false; } else if (i < 0 || i >= count) { count++; arr[count - 1] = t; return true; } else if (i >= 0 && i<count) { count++; for (int x = count - 1; x > i; x--) arr[x] = arr[x - 1]; arr[i] = t; return true; } else return false; } template <typename T, int n> bool Arr<T, n>::Delete(int i) { if (i < 0 && count>0) { count--; return true; } else if (i >= 0 && count>0 && i<count) { count--; for (int x = i; x < count; x++) arr[x] = arr[x + 1]; return true; } else return false; } template <typename T, int n> bool Arr<T, n>::Reset(const T&t, int i) { if (i >= 0 && i < count) { arr[i] = t; return true; } else return false; } template <typename T, int n> int Arr<T, n>::Search(const T&t) { for (int i = 0; i<count; i++) if (arr[i] == t) { return i; } return -1; } template <typename T, int n> int Arr<T, n>::Size() { return count; }
//!使用模板實現一個自定義的陣列類, //!能夠新增、修改、刪除、使用下標訪問資料、 //!根據資料返回資料組中第一個符合的小標、 //!獲取陣列中當前儲存的資料個數 #pragma once #include <iostream> template <typename T, int n> class Array { private: int count=n; T *ar; T *temp; public: int theN() { return n; } Array() { ar = new T[count]; temp = nullptr; } explicit Array(const T&t);//初始化為同一個數值 ~Array() { delete[]ar; ar = nullptr; if (nullptr!=temp) { delete[]temp; } temp = nullptr; } //下標訪問 T&operator[](int i); //插入or增加一個元素 bool Add(const T&t, int i = -1); //重置某個元素 bool Reset(const T&t,int i); //刪除某個元素 bool Delete(int i=-1); //查詢某個元素第一個下標 int Search(const T&t); //查詢陣列長度 int Size(); }; template <typename T, int n> Array<T, n>::Array(const T&t) { ar = new T[count]; for (int i = 0; i < n; i++) ar[i] = t; } template <typename T, int n> T& Array<T, n>::operator[](int i) { if (i<0||i >= count) { throw "out of limits"; } return ar[i]; } template <typename T, int n> bool Array<T, n>::Add(const T&t,int i) { if (i < 0||i>=count ) { count++; temp = new T[count]; for (int x = 0; x < count-1; x++) temp[x] = ar[x]; delete []ar; ar = new T[count]; for (int x = 0; x < count-1; x++) ar[x] = temp[x]; ar[count - 1] = t; delete []temp; temp = nullptr; return true; } else if (i >= 0 && i<count) { count++; temp = new T[count]; for (int x = 0; x <i; x++) temp[x] = ar[x]; temp[i] = t; for (int x = i+1; x<count; x++) temp[x] = ar[x-1]; delete []ar; ar = new T[count]; for (int x = 0; x < count; x++) ar[x] = temp[x]; delete []temp; temp = nullptr; return true; } else return false; } template <typename T, int n> bool Array<T, n>::Reset(const T&t,int i) { if (i >= 0 && i < count) { ar[i] = t; return true; } else return false; } template <typename T, int n> bool Array<T, n>::Delete( int i) { if (i < 0&&count>0) { count--; temp = new T[count]; for (int x = 0; x < count; x++) temp[x] = ar[x]; delete []ar; ar = new T[count]; for (int x = 0; x < count; x++) ar[x] = temp[x]; delete []temp; temp = nullptr; return true; } else if(i>=0&&count>0&&i<count) { count--; temp = new T[count]; for (int x = 0; x < i; x++) temp[x] = ar[x]; for(int x=i;x<count;x++) temp[x] = ar[x+1]; delete []ar; ar = new T[count]; for (int x = 0; x < count; x++) ar[x] = temp[x]; delete []temp; temp = nullptr; return true; } else return false; } template <typename T, int n> int Array<T, n>::Search(const T&t) { for(int i=0;i<count;i++) if (ar[i] == t) { return i; } //?throw "not find"; return -1; } template <typename T, int n> int Array<T, n>::Size() { return count; }