Simple vector implementation

Source: Internet
Author: User
# 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.