动态数组

// Test.cpp : Defines the entry point for the console application.
 //
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
template <class T> class Array
{
private:
int Length;//数组大小
 12T *list;//指向数组第一地址
 13
 14public:
 15
 16     Array()
 17    {
 18            Length=0;
 19            list = new T[1];
 20    }
 21    ~Array()
 22    {    Length=0;
 23        delete []list;
 24    }
 25
 26    //类型转换
 27operator T*() const
 28{
 29
 30 return this->list;
 31}
 32//--重栽=
 33Array<T>& operator=(const Array<T>& x)
 34{
 35   delete []list;
 36   tist->Length=x.Length;
 37
 38   list=new T[this->Length];
 39
 40   for(int i=0;i<Length;i++)
 41   {
 42       list[i]=x.list[i];
 43
 44   }
 45}
 46
 47
 48//---可以 像访问元素那样访问对象
 49T& operator[](int Index)
 50{
 51
 52 return this->list[Index];
 53}
 54
 55
 56    //--添加一个数据
 57    void  Add(T inputT)
 58    { 
 59        int newSize=Length+1;
 60        this->Resize(newSize,0,0);
 61        list[Length-1]=inputT;
 62    }
 63
 64
 65    //--输入1删除的就是索引为1的元素
 66   void Remove(int RemoveIndex)
 67   {
 68
 69       if(Length<=RemoveIndex||RemoveIndex<0)
 70           return;
 71    this->Resize(Length-1,2,RemoveIndex);
 72  
 73   }
 74
 75//插入一个数据
 76void Insert(int InsertIndex,T inputT)
 77{
 78//Length=Length+1;
 79 this->Resize(Length+1,1,InsertIndex);
 80 this->list[InsertIndex]=inputT;
 81//Length=Length-1;
 82}
 83  
 84int GetLength()
 85{
 86
 87return this->Length;
 88}
 89
 90//-------------新的大小,--状态0代表放大或者缩小数组,1代表插入,2代表缩小
 91//--StateIndex是修改放大或者缩小的索引
 92    void Resize(int newSize,int State,int StateIndex)
 93    {
 94   
 95        if(newSize<=0)
 96            return;
 97   
 98        if(newSize!=Length)
 99        {
100            //---设置一个临时拷贝数组
101            T *newList=new T[newSize];
102
103            //--n是防止超范围复制
104  
105
106             if(State==0)
107             {
108            
109                 n=newSize;
110             }
111            else if(State==1)
112             {//--插入数据的时候最终循环的数量是,新数组的数量,插入的多
113               n=newSize;
114             }
115             else if(State==2)
116             {
117             //--删除数据循环一多的为准
118                 n=Length;
119             }
120
121             //--根据State来循环拷贝数据
122             for(int i=0;i<n;i++)
123             {
124                 //--正常拷贝
125                 if(State==0)
126                 {
127                        newList[i]=list[i];
128                 }else if(State==1)
129                 {
130                     //--这里是插入操作用的
131                
132                     if(i<StateIndex)
133                    {
134                        //--循环小于插入点的时候正常复制
135                        newList[i]=list[i];
136                    }
137                    else
138                    {
139                        //--StateIndex!=i表示空出插入点
140                        //--这段代码代表了假如我不要索引是0的的元素那么我直接在i=0的时候把原数组中
141                        //-i=1的数据取过来总之既然是删除那么代表不需要删除的那个元素哈哈
142                        newList[i]=list[i-1];
143                    }
144               
145                
146                 }else if(State==2)
147                 {
148                 //--删除
149                     if(i<StateIndex)
150                    {
151                        //--如果要删除第下标为1的元素
152                        //--当i=0的时候由于i小于删除下标那么进入该判断远洋复制
153                        newList[i]=list[i];
154                    }
155                    else
156                    {
157                        //--还是上面的问题当我要删除第下标为1的元素
158                        //--i=1这时候由于i不再小于删除下标,进入这里
159                        //--删除一个元素原则上就是取他下一个,由于循环变量
160                        //--是新数组比老数组长度小1这样就相当于删除了一个元素
161                        newList[i]=list[i+1];
162                    }
163                
164                
165                 }
166            
167
168
169
170
171
172
173             }
174        //--删除元时候数组
175        delete []list;
176        //--复制数组
177        list=newList;
178        //--重新指定长度
179        Length=newSize;
180
181
182
183        }
184
185    }
186
187};
188
189
190
191
192
193
194
195
196
197int main(int argc, char* argv[])
198{
199Array<int> test1;
200test1.Add(1);
201test1.Add(2);
202test1.Add(3);
203test1.Add(4);
204test1.Add(5);
205test1.Add(6);
206for(int i=0;i<test1.GetLength();i++)
207{
208
209    cout<<test1[i]<<endl;
210
211}
212
213test1.Remove(0);
214test1.Remove(test1.GetLength()-1);
215
216cout<<"删除0,和最后一个参数"<<endl;
217for(int i2=0;i2<test1.GetLength();i2++)
218{
219
220    cout<<test1[i2]<<endl;
221
222}
223
224
225test1.Insert(0,9999);
226//test1.Insert(test1.GetLength()-1,888888);
227cout<<"插入参数测试"<<endl;
228//cout<<test1.GetLength()<<endl;
229for(int i3=0;i3<test1.GetLength();i3++)
230{
231
232    cout<<test1[i3]<<endl;
233
234}
235
236
237    return 0;
238}
239
240
241

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值