C++基础学习笔记:自定义数组模板类

来源:互联网 发布:苹果手机电池修复软件 编辑:程序博客网 时间:2022/08/11 15:59
//!时间: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的4test.Reset(4, 2);//将位2设置为4cout << 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 oncetemplate <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;}