Skip to content

Commit 90efe5f

Browse files
authored
Update DynArray.h
fix bug
1 parent c8b8195 commit 90efe5f

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

DataStruct/Array/DynArray.h

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace AlLib
1414
{
1515
namespace Array
1616
{
17-
// 突破固定容量数组限制
18-
// 容量可以动态增加和减少的数组
19-
// 适合作为管理动态元素集合的容器
17+
// 突破固定容量数组限制
18+
// 容量可以动态增加和减少的数组
19+
// 适合作为管理动态元素集合的容器
2020
template<typename T>
2121
class DynArray
2222
{
@@ -79,7 +79,7 @@ namespace AlLib
7979
void Shrink();
8080

8181
private:
82-
T* m_pSource;// 当T为引用类型时,sizeof无法获得其真实大小
82+
T* m_pSource;// 当T为引用类型时,sizeof无法获得其真实大小
8383
int m_nSize;
8484
int m_nCapacity;
8585

@@ -206,21 +206,11 @@ namespace AlLib
206206
m_alloc.destroy(--_pEnd);
207207
}
208208

209-
// 内存释放
209+
// 内存释放
210210
m_alloc.deallocate(m_pSource, m_nCapacity);
211211
m_pSource = nullptr;
212212
m_nSize = 0;
213213
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-
}
224214
}
225215

226216
template<typename T>
@@ -279,14 +269,14 @@ namespace AlLib
279269

280270
Check(m_nSize + 1);
281271
m_alloc.construct(m_pSource + m_nSize, value_);
282-
m_nSize++;
283-
272+
284273
for (int _i = m_nSize - 1; _i >= nIndex_; _i--)
285274
{
286275
*(m_pSource + _i + 1) = *(m_pSource + _i);
287276
}
288277

289278
*(m_pSource + nIndex_) = value_;
279+
m_nSize++;
290280
}
291281

292282
template<typename T>
@@ -298,13 +288,13 @@ namespace AlLib
298288
return;
299289
}
300290

301-
// 前移
291+
// 前移
302292
for (int _i = nIndex_ + 1; _i < m_nSize; _i++)
303293
{
304294
*(m_pSource + _i - 1) = *(m_pSource + _i);
305295
}
306296

307-
m_alloc.destroy(m_pSource + m_nSize);
297+
m_alloc.destroy(m_pSource + m_nSize - 1);
308298
m_nSize--;
309299
if (m_nSize <= m_nCapacity / 4)
310300
{

0 commit comments

Comments
 (0)