1 #include"iostream" 2 #include"stdio.h" 3 #include"algorithm" 4 #include"map" 5 using namespace std; 6 template<typename T> 7 struct TContainer 8 { 9 virtual void push(const T&)=0; 10 virtual void pop()=0; 11 virtual const T& begin()=0; 12 virtual const T& end()=0; 13 virtual size_t size()=0; 14 }; 15 template<typename T> 16 struct TVector:public TContainer<T> 17 { 18 static const size_t _step=100; 19 TVector() 20 { 21 _size=0; 22 _cap=_step; 23 buf=0; 24 re_capacity(_cap); 25 } 26 ~TVector() 27 { 28 free(buf); 29 } 30 void re_capacity(size_t s) 31 { 32 if(!buf) 33 buf=(T*)malloc(sizeof(T)*s); 34 else 35 buf=(T*)realloc(buf,sizeof(T)*s); 36 } 37 virtual void push(const T& v) 38 { 39 if(_size>=_cap) 40 { 41 re_capacity(_cap+=_step); 42 } 43 buf[_size++]=v; 44 } 45 virtual void pop() 46 { 47 if(_size) 48 _size--; 49 } 50 virtual const T& begin() 51 { 52 return buf[0]; 53 } 54 virtual const T& end() 55 { 56 if(_size) 57 return buf[_size-1]; 58 } 59 virtual size_t size() 60 { 61 return _size; 62 } 63 const T& operator[](size_t i) 64 { 65 if(i>=0&&i<_size) 66 return buf[i]; 67 } 68 private: 69 size_t _size; 70 size_t _cap; 71 T* buf; 72 }; 73 74 int main() 75 { 76 TVector<int>v; 77 for(int i=0;i<100;i++) 78 v.push(i); 79 for(int i=0;i<100;i++) 80 cout<<v[i]<<endl; 81 return 0; 82 }