数据结构--变长数组
刚发现,以前用vector觉得挺简单的,自己实现一下才知道这么麻烦。真是佩服那些C++大师,写出这么好的东西。
这算是一个简易的vector吧,之所以说它简易,并不是因为功能少,而是实现的复杂度远远不及std::vector
但基本原理是一样的
1 #pragma once
2
3 const int INIT_ARRAY_SIZE = 10;
4 template<typename T>
5 class Array
6 {
7 public:
8 struct Iterator {
9
10 Iterator(T* ptr):m_ptr(ptr){}
11
12 T& operator*()
13 {
14 return (*m_ptr);
15 }
16
17 T* operator->()
18 {
19 return (&**this);
20 }
21
22 Iterator& operator++()
23 {
24 ++m_ptr;
25 return *this;
26 }
27 Iterator operator++(int)
28 {
29 Iterator temp = *this;
30 ++(*this);
31 return temp;
32 }
33
34 Iterator& operator--()
35 {
36 --m_ptr;
37 return *this;
38 }
39 Iterator operator--(int)
40 {
41 Iterator temp = *this;
42 --(*this);
43 return temp;
44 }
45
46 bool operator!=(const Iterator &it)
47 {
48 return m_ptr != it.m_ptr;
49 }
50
51 bool operator==(const Iterator &it)
52 {
53 return m_ptr == it.m_ptr;
54 }
55
56 bool operator<(const Iterator &it)
57 {
58 return m_ptr<it.m_ptr;
59 }
60
61 Iterator operator+(size_t off) const
62 {
63 Iterator tmp = *this;
64 tmp.m_ptr+=off;
65 return tmp;
66 }
67
68
69
70 Iterator operator-(size_t off)
71 {
72 Iterator tmp = *this;
73 tmp.m_ptr-=off;
74 return tmp;
75 }
76
77 size_t operator-(const Iterator& it)
78 {
79 return m_ptr - it.m_ptr;
80 }
81
82
83 T* m_ptr;
84 };
85 Array()
86 : m_head(new T[INIT_ARRAY_SIZE]),
87 m_last(m_head),
88 m_end(m_head+INIT_ARRAY_SIZE-1)
89 {}
90
91 Array(size_t t)
92 : m_head(new T[INIT_ARRAY_SIZE<t ? t : INIT_ARRAY_SIZE]),
93 m_last(m_head+t),
94 m_end(m_head+(INIT_ARRAY_SIZE<t ? t-1 : INIT_ARRAY_SIZE-1))
95 {}
96
97 T& operator[](size_t t)
98 {
99 return *(m_head+t);
100 }
101
102 size_t size()
103 {
104 return m_head == 0 ? 0 : m_last-m_head/* / sizeof(T)*/;
105 }
106
107
108
109 void push_back(const T& data)
110 {
111 size_t s;
112 s = size();
113 s = capacity();
114 if(size() <capacity())
115 {
116 m_head[size()] = data;
117 m_last++;
118 }
119 else//需要扩充长度
120 {
121 insert(end(), data);
122 }
123 }
124
125 Iterator insert(Iterator _Where, const T& data)
126 {
127 size_t off = _Where - begin();
128 _Insert(_Where, 1, data);
129 return begin()+off;
130 }
131
132 Iterator begin()
133 {
134 return m_head;
135 }
136
137 Iterator end()
138 {
139 return m_last;
140 }
141
142 bool empty()
143 {
144 return size() == 0;
145 }
146
147 void remove(Iterator _Where)
148 {
149 if(empty())
150 {
151 return;
152 }
153
154 size_t off = _Where-begin();
155
156
157
158 _move(_Where+1, end(), m_head+off);
159 m_last--;
160 }
161
162 private:
163
164
165 size_t capacity()
166 {
167 return m_head == 0 ? 0 : m_end-m_head;
168 }
169
170 void _Insert(Iterator _Where, size_t nCount, const T& data)
171 {
172
173 if(capacity() - size() < nCount)
174 {
175
176 size_t oldSize = size();
177 size_t Capacity = capacity();
178 size_t newSize = (Capacity+1)*1.5;
179 if (newSize < size() + nCount)
180 {
181 newSize = size() + nCount;
182 }
183
184 T* temp = new T[newSize];
185
186 T* t = temp;
187 Iterator it = begin();
188 t = _move(begin(), _Where, t);
189
190 t = _fill(t, t+nCount, data);
191
192 _move(_Where, end(), t);
193
194 delete[] m_head;
195
196 m_head = temp;
197 m_last = m_head + oldSize + nCount;
198 m_end = temp + newSize - 1;
199
200 }
201 else if(end() - _Where < nCount)
202 {
203 T* p = _move(_Where, m_last, _Where.m_ptr+nCount);
204
205
206 _fill(_Where, _Where+nCount, data);
207 m_last+=nCount;
208 }
209 else
210 {
211 T* oldEnd = m_last;
212
213
214 _copy_backward(_Where, oldEnd, oldEnd+nCount);
215
216
217
218 _fill(_Where, _Where+nCount, data);
219 m_last += nCount;
220
221 }
222 }
223
224 T* _move(Iterator _First, Iterator _Last, T* p)
225 {
226 for(; _First != _Last; )
227 {
228 *p++= *_First++;
229
230 }
231
232 return p;
233 }
234
235 void _copy_backward(Iterator _First, Iterator _Last, T* p)
236 {
237 for(; !(_Last < _First); )
238 {
239 *p-- = *_Last--;
240 }
241 }
242
243 T* _fill(Iterator _First, Iterator _Last, const T& data)
244 {
245 for(; _First != _Last; )
246 {
247 *_First++ = data;
248 }
249
250 return _First.m_ptr;
251 }
252
253 T* m_head;
254 T* m_last;
255 T* m_end;
256 };
2
3 const int INIT_ARRAY_SIZE = 10;
4 template<typename T>
5 class Array
6 {
7 public:
8 struct Iterator {
9
10 Iterator(T* ptr):m_ptr(ptr){}
11
12 T& operator*()
13 {
14 return (*m_ptr);
15 }
16
17 T* operator->()
18 {
19 return (&**this);
20 }
21
22 Iterator& operator++()
23 {
24 ++m_ptr;
25 return *this;
26 }
27 Iterator operator++(int)
28 {
29 Iterator temp = *this;
30 ++(*this);
31 return temp;
32 }
33
34 Iterator& operator--()
35 {
36 --m_ptr;
37 return *this;
38 }
39 Iterator operator--(int)
40 {
41 Iterator temp = *this;
42 --(*this);
43 return temp;
44 }
45
46 bool operator!=(const Iterator &it)
47 {
48 return m_ptr != it.m_ptr;
49 }
50
51 bool operator==(const Iterator &it)
52 {
53 return m_ptr == it.m_ptr;
54 }
55
56 bool operator<(const Iterator &it)
57 {
58 return m_ptr<it.m_ptr;
59 }
60
61 Iterator operator+(size_t off) const
62 {
63 Iterator tmp = *this;
64 tmp.m_ptr+=off;
65 return tmp;
66 }
67
68
69
70 Iterator operator-(size_t off)
71 {
72 Iterator tmp = *this;
73 tmp.m_ptr-=off;
74 return tmp;
75 }
76
77 size_t operator-(const Iterator& it)
78 {
79 return m_ptr - it.m_ptr;
80 }
81
82
83 T* m_ptr;
84 };
85 Array()
86 : m_head(new T[INIT_ARRAY_SIZE]),
87 m_last(m_head),
88 m_end(m_head+INIT_ARRAY_SIZE-1)
89 {}
90
91 Array(size_t t)
92 : m_head(new T[INIT_ARRAY_SIZE<t ? t : INIT_ARRAY_SIZE]),
93 m_last(m_head+t),
94 m_end(m_head+(INIT_ARRAY_SIZE<t ? t-1 : INIT_ARRAY_SIZE-1))
95 {}
96
97 T& operator[](size_t t)
98 {
99 return *(m_head+t);
100 }
101
102 size_t size()
103 {
104 return m_head == 0 ? 0 : m_last-m_head/* / sizeof(T)*/;
105 }
106
107
108
109 void push_back(const T& data)
110 {
111 size_t s;
112 s = size();
113 s = capacity();
114 if(size() <capacity())
115 {
116 m_head[size()] = data;
117 m_last++;
118 }
119 else//需要扩充长度
120 {
121 insert(end(), data);
122 }
123 }
124
125 Iterator insert(Iterator _Where, const T& data)
126 {
127 size_t off = _Where - begin();
128 _Insert(_Where, 1, data);
129 return begin()+off;
130 }
131
132 Iterator begin()
133 {
134 return m_head;
135 }
136
137 Iterator end()
138 {
139 return m_last;
140 }
141
142 bool empty()
143 {
144 return size() == 0;
145 }
146
147 void remove(Iterator _Where)
148 {
149 if(empty())
150 {
151 return;
152 }
153
154 size_t off = _Where-begin();
155
156
157
158 _move(_Where+1, end(), m_head+off);
159 m_last--;
160 }
161
162 private:
163
164
165 size_t capacity()
166 {
167 return m_head == 0 ? 0 : m_end-m_head;
168 }
169
170 void _Insert(Iterator _Where, size_t nCount, const T& data)
171 {
172
173 if(capacity() - size() < nCount)
174 {
175
176 size_t oldSize = size();
177 size_t Capacity = capacity();
178 size_t newSize = (Capacity+1)*1.5;
179 if (newSize < size() + nCount)
180 {
181 newSize = size() + nCount;
182 }
183
184 T* temp = new T[newSize];
185
186 T* t = temp;
187 Iterator it = begin();
188 t = _move(begin(), _Where, t);
189
190 t = _fill(t, t+nCount, data);
191
192 _move(_Where, end(), t);
193
194 delete[] m_head;
195
196 m_head = temp;
197 m_last = m_head + oldSize + nCount;
198 m_end = temp + newSize - 1;
199
200 }
201 else if(end() - _Where < nCount)
202 {
203 T* p = _move(_Where, m_last, _Where.m_ptr+nCount);
204
205
206 _fill(_Where, _Where+nCount, data);
207 m_last+=nCount;
208 }
209 else
210 {
211 T* oldEnd = m_last;
212
213
214 _copy_backward(_Where, oldEnd, oldEnd+nCount);
215
216
217
218 _fill(_Where, _Where+nCount, data);
219 m_last += nCount;
220
221 }
222 }
223
224 T* _move(Iterator _First, Iterator _Last, T* p)
225 {
226 for(; _First != _Last; )
227 {
228 *p++= *_First++;
229
230 }
231
232 return p;
233 }
234
235 void _copy_backward(Iterator _First, Iterator _Last, T* p)
236 {
237 for(; !(_Last < _First); )
238 {
239 *p-- = *_Last--;
240 }
241 }
242
243 T* _fill(Iterator _First, Iterator _Last, const T& data)
244 {
245 for(; _First != _Last; )
246 {
247 *_First++ = data;
248 }
249
250 return _First.m_ptr;
251 }
252
253 T* m_head;
254 T* m_last;
255 T* m_end;
256 };