#include<iostream>
using namespace std;
template<typename T>
class Myvector
{
public:
Myvector()
{
data=NULL;
capacity=size=0;
}
Myvector(const Myvector& other)
{
if(this==&other)
return;
data=new T[other.size];
for(int i=0; i<size; i++)
data[i]=other[i];
capacity=other.capacity;
size=other.size;
}
T& operator[](int index)
{
if(index>=size)
{
cout<<"Overflow!"<<endl;
}
return data[index];
}
const Myvector& push_back(const T temp)
{
if(size>=capacity)
{
T* newData=new T[capacity*2+1];
memmove(newData,data,size*sizeof(T));
delete[] data;
data=newData;
capacity=capacity*2+1;
}
data[size++]=temp;
return *this;
}
unsigned int GetSize()
{
return size;
}
unsigned int GetCapacity()
{
return capacity;
}
private:
T* data;
unsigned int c
面试手写一个简单的STL容器vector
最新推荐文章于 2023-11-13 18:32:06 发布