#include <iostream>
template <typename T>
class MyVector
{
T* Array_;
size_t Size_;
size_t Capacity_;
public:
MyVector()
{
Array_ = new T[2 * sizeof(T)];
Capacity_ = 2 * sizeof(T);
Size_ = 0;
}
~MyVector() { }
size_t Size() const
{
return Size_;
}
void Release()
{
delete Array_;
Array_ = nullptr;
}
void Resize()
{
T* TempArray_;
TempArray_ = new T[Capacity_ * 2 * sizeof(T)];
for (size_t i = 0; i < Size_; ++i)
{
TempArray_[i] = Array_[i];
}
Release();
Array_ = TempArray_;
Capacity_ = 2 * Capacity_;
}
void PushBack(T _Data)
{
if (Capacity_ <= Size_)
{
Resize();
}
Array_[Size_] = _Data;
++Size_;
}
T& operator[](size_t _Index)
{
return Array_[_Index];
}
};
int main()
{
MyVector<int> IntArr = {};
IntArr.PushBack(1);
IntArr.PushBack(2);
IntArr.PushBack(3);
for (size_t i = 0; i < IntArr.Size(); ++i)
{
std::cout << IntArr[i] << std::endl;
}
}
둥지/알고리즘