一维动态数组类模板
非常怀念c#的数组,于是在资料的帮助下有下面的代码呵呵

Code
1
// Test.cpp : Defines the entry point for the console application.
2
//
3
#include "stdafx.h"
4
#include <iostream>
5
#include <cstring>
6
#include <string>
7
using namespace std;
8
template <class T> class Array
9

{
10
private:
11
int Length;//数组大小
12
T *list;//指向数组第一地址
13
14
public:
15
16
     Array()
17
    
{
18
            Length=0;
19
            list = new T[1];
20
    }
21
    ~Array()
22
    
{    Length=0;
23
        delete []list;
24
    }
25
26
    //类型转换
27
operator T*() const
28

{
29
30
 return this->list;
31
}
32
//--重栽=
33
Array<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
//---可以 像访问元素那样访问对象
49
T& 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
//插入一个数据
76
void 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
   
84
int GetLength()
85

{
86
87
return 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
197
int main(int argc, char* argv[])
198

{
199
Array<int> test1;
200
test1.Add(1);
201
test1.Add(2);
202
test1.Add(3);
203
test1.Add(4);
204
test1.Add(5);
205
test1.Add(6);
206
for(int i=0;i<test1.GetLength();i++)
207

{
208
209
    cout<<test1[i]<<endl;
210
211
}
212
213
test1.Remove(0);
214
test1.Remove(test1.GetLength()-1);
215
216
cout<<"删除0,和最后一个参数"<<endl;
217
for(int i2=0;i2<test1.GetLength();i2++)
218

{
219
220
    cout<<test1[i2]<<endl;
221
222
}
223
224
225
test1.Insert(0,9999);
226
//test1.Insert(test1.GetLength()-1,888888);
227
cout<<"插入参数测试"<<endl;
228
//cout<<test1.GetLength()<<endl;
229
for(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
1
// Test.cpp : Defines the entry point for the console application.2
//3
#include "stdafx.h"4
#include <iostream>5
#include <cstring>6
#include <string>7
using namespace std;8
template <class T> class Array9


{10
private:11
int Length;//数组大小12
T *list;//指向数组第一地址13

14
public:15

16
     Array()17

    
{18
            Length=0;19
            list = new T[1];20
    }21
    ~Array()22

    
{    Length=0;23
        delete []list;24
    }25

26
    //类型转换27
operator T*() const28


{29

30
 return this->list;31
}32
//--重栽=33
Array<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
//---可以 像访问元素那样访问对象49
T& 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
//插入一个数据76
void 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
   84
int GetLength()85


{86

87
return 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
                    else156

                    
{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

197
int main(int argc, char* argv[])198


{199
Array<int> test1;200
test1.Add(1);201
test1.Add(2);202
test1.Add(3);203
test1.Add(4);204
test1.Add(5);205
test1.Add(6);206
for(int i=0;i<test1.GetLength();i++)207


{208

209
    cout<<test1[i]<<endl;210

211
}212

213
test1.Remove(0);214
test1.Remove(test1.GetLength()-1);215

216
cout<<"删除0,和最后一个参数"<<endl;217
for(int i2=0;i2<test1.GetLength();i2++)218


{219

220
    cout<<test1[i2]<<endl;221

222
}223

224

225
test1.Insert(0,9999);226
//test1.Insert(test1.GetLength()-1,888888);227
cout<<"插入参数测试"<<endl;228
//cout<<test1.GetLength()<<endl;229
for(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

                    
                
                
            
        
浙公网安备 33010602011771号