# Include <iostream> "https://github.com/fookwood/fkcode/network" template <Class Object> class vector {PRIVATE: int thesize; // number of elements int thecapacity; // capacity object * objects; // point to the public: explicit vector (INT initsize = 0): thesize (initsize), thecapacity (initsize + space_capacity) {objects = new object [thecapacity];} // use explicit to avoid implicit conversions of vector (const vector & RHs): Objects (null) {operator = (RHs);}/C Opy constructor ~ Vector () {Delete [] objects;} const vector & operator = (const vector & RHs) {If (this! = & RHs) {// Delete [] objects; thesize = RHS. size (); thecapacity = RHS. capacity (); objects = new object [capacity ()]; for (int K = 0; k <thesize; k ++) // copy element objects [k] = RHS. objects [k];} return * This;} // above, the "Big Three" Void resize (INT newsize) {If (newsize> thecapacity) reserve (newsize * 2 + 1); // thesize = newsize;} // when there is not enough space, the current space is doubled, + 1 is used to prevent zero void reserve (INT ne Wcapacity) {If (newcapacity <thesize) return; object * oldarray = objects; objects = new object [newcapacity]; for (int K = 0; k <thesize; k ++) objects [k] = oldarray [k]; thecapacity = newcapacity; Delete [] oldarray;} // obtain new space, copy, and release the old // [] operator, non-const and const versions .. object & operator [] (INT index) {return objects [Index];} const object & operator [] (INT index) const {return objects [Index];} bool E Mpty () const {return size () = 0;} int size () const {return thesize;} int capacity () const {return thecapacity ;} void push_back (const object & X) {If (thesize = thecapacity) Reserve (2 * thecapacity + 1); objects [thesize ++] = x ;} // insert element void pop_back () {thesize --;} const object & back () const {return objects [theSize-1];} typedef object * iterator; // replace typedef const object * const_ite with a native pointer Rator; iterator begin () {return & objects [0];} const_iterator begin () const {return & objects [0];} iterator end () {return & objects [thesize];} const_iterator end () const {return & objects [thesize];} Enum {space_capacity = 4}; // initial capacity}; int main () {// perform a simple test of vector V; For (INT I = 0; I <100; I ++) v. push_back (I); For (vector: iterator it = v. begin (); it! = V. end (); It ++) STD: cout <* It <STD: Endl; const vector V (V); For (vector: const_iterator it = v. begin (); it! = V. End (); It ++) STD: cout <* It <STD: Endl; return 0 ;}
http://www.fookwood.com/archives/455