注释析构函数保证运行正常,再仔细考虑下这个

This commit is contained in:
iridiumR 2022-03-12 17:22:35 +08:00
parent 342c437437
commit af7d67b808

View file

@ -2,7 +2,7 @@
* @Author: iR * @Author: iR
* @Date: 2022-03-11 16:47:31 * @Date: 2022-03-11 16:47:31
* @LastEditors: iR * @LastEditors: iR
* @LastEditTime: 2022-03-12 14:08:48 * @LastEditTime: 2022-03-12 17:12:04
* @FilePath: \Code\inc\vector.hpp * @FilePath: \Code\inc\vector.hpp
* @Description: * @Description:
* *
@ -40,7 +40,7 @@ public:
Vector(int len, T value); Vector(int len, T value);
//析构函数 //析构函数
~Vector() { delete[] _v; } // ~Vector() { delete[] _v; }
//获取值 //获取值
T &get(int a); T &get(int a);
@ -83,15 +83,15 @@ public:
template <class T> template <class T>
Vector<T>::Vector() Vector<T>::Vector()
{ {
_used = 1; _used = 0;
_len = 2; _len = 10;
_v = new T[_len]; _v = new T[_len];
} }
template <class T> template <class T>
Vector<T>::Vector(int len) Vector<T>::Vector(int len)
{ {
_used = len; _used = 0;
_len = 2 * len; _len = 2 * len;
_v = new T[_len]; _v = new T[_len];
} }
@ -102,7 +102,7 @@ Vector<T>::Vector(int len, T value)
_used = len; _used = len;
_len = 2 * len; _len = 2 * len;
_v = new T[_len]; _v = new T[_len];
for (int i = 0; i < _len; i++) for (int i = 0; i < _used; i++)
{ {
_v[i] = value; _v[i] = value;
} }
@ -146,7 +146,7 @@ T &Vector<T>::operator[](int a)
catch (const char *msg) catch (const char *msg)
{ {
std::cout << msg; std::cout << msg;
return 0; return _v[0];
} }
} }
@ -178,11 +178,12 @@ void Vector<T>::expandTo(int a)
template <class T> template <class T>
void Vector<T>::expand() void Vector<T>::expand()
{ {
int old_len = _len;
_len = _len << 1; int old_used = _used;
_len = _len * 2;
T *p = new T[_len]; T *p = new T[_len];
for (int i = 0; i < old_len; i++) for (int i = 0; i < old_used; i++)
p[i] = _v[i]; p[i] = _v[i];
delete[] _v; delete[] _v;
@ -192,7 +193,7 @@ void Vector<T>::expand()
template <class T> template <class T>
void Vector<T>::shrink() void Vector<T>::shrink()
{ {
_len = _len >> 1; _len = _len / 2;
T *p = new T[_len]; T *p = new T[_len];
for (int i = 0; i < _used; i++) for (int i = 0; i < _used; i++)