File tree Expand file tree Collapse file tree 1 file changed +9
-19
lines changed Expand file tree Collapse file tree 1 file changed +9
-19
lines changed Original file line number Diff line number Diff line change @@ -14,9 +14,9 @@ namespace AlLib
14
14
{
15
15
namespace Array
16
16
{
17
- // 突破固定容量数组限制
18
- // 容量可以动态增加和减少的数组
19
- // 适合作为管理动态元素集合的容器
17
+ // 突破固定容量数组限制
18
+ // 容量可以动态增加和减少的数组
19
+ // 适合作为管理动态元素集合的容器
20
20
template <typename T>
21
21
class DynArray
22
22
{
@@ -79,7 +79,7 @@ namespace AlLib
79
79
void Shrink ();
80
80
81
81
private:
82
- T* m_pSource;// 当T为引用类型时,sizeof无法获得其真实大小
82
+ T* m_pSource;// 当T为引用类型时,sizeof无法获得其真实大小
83
83
int m_nSize;
84
84
int m_nCapacity;
85
85
@@ -206,21 +206,11 @@ namespace AlLib
206
206
m_alloc.destroy (--_pEnd);
207
207
}
208
208
209
- // 内存释放
209
+ // 内存释放
210
210
m_alloc.deallocate (m_pSource, m_nCapacity);
211
211
m_pSource = nullptr ;
212
212
m_nSize = 0 ;
213
213
m_nCapacity = 0 ;
214
- m_pSource = m_alloc.allocate (100 );
215
- if (m_pSource == nullptr )
216
- {
217
- throw " out of memory" ;
218
- }
219
- else
220
- {
221
- m_nSize = 0 ;
222
- m_nCapacity = 100 ;
223
- }
224
214
}
225
215
226
216
template <typename T>
@@ -279,14 +269,14 @@ namespace AlLib
279
269
280
270
Check (m_nSize + 1 );
281
271
m_alloc.construct (m_pSource + m_nSize, value_);
282
- m_nSize++;
283
-
272
+
284
273
for (int _i = m_nSize - 1 ; _i >= nIndex_; _i--)
285
274
{
286
275
*(m_pSource + _i + 1 ) = *(m_pSource + _i);
287
276
}
288
277
289
278
*(m_pSource + nIndex_) = value_;
279
+ m_nSize++;
290
280
}
291
281
292
282
template <typename T>
@@ -298,13 +288,13 @@ namespace AlLib
298
288
return ;
299
289
}
300
290
301
- // 前移
291
+ // 前移
302
292
for (int _i = nIndex_ + 1 ; _i < m_nSize; _i++)
303
293
{
304
294
*(m_pSource + _i - 1 ) = *(m_pSource + _i);
305
295
}
306
296
307
- m_alloc.destroy (m_pSource + m_nSize);
297
+ m_alloc.destroy (m_pSource + m_nSize - 1 );
308
298
m_nSize--;
309
299
if (m_nSize <= m_nCapacity / 4 )
310
300
{
You can’t perform that action at this time.
0 commit comments