C ++ Vector will also be used for simple implementation.

Source: Internet
Author: User

C ++ Vector will also be used for simple implementation.

We know that the size of the memory block cannot be changed, so the size of the array cannot be changed. But the STL vector frees us from this problem. It can help us dynamically manage the array size.

It is true that the bottom layer of STL vector is implemented through dynamic arrays. When the array size is not enough, a larger memory will be applied and the original element value will be copied, delete the original small memory, of course, this operation is very expensive, including a memory request, memory release, and element initialization.

This article provides the implementation of the Vector Template, which is far behind the STL vector Template, but the general principle is almost the same, read the following code to deepen your understanding of STL vector.

 

 

template
 
  class Vector{public:
 
// Constructor, copy constructor and destructor Vector (int size = 0): theSize (size), theCapacity (0 + SPACE_CAPACITY) {objects = new T [theCapacity];} Vector (const Vector & rhs): objects (NULL) {operator = (rhs );}~ Vector () {delete [] objects;} // overload = Number operator const Vector & operator = (const Vector & rhs) {theCapacity = rhs. theCapacity; theSize = rhs. theSize; objects = new objects [this-> theCapacity]; for (int I = 0; I
 
  
TheSize; I ++) objects [I] = rhs. objects [I]; return * this;} // adjust sizevoid resize (int newSize) {if (newSize> theCapacity) reserve (newSize * 2 + 1); theSize = newSize ;}
 
// Adjust the reserved space, that is, the size of the actually applied memory void reserve (int newCapacity) {if (newCapacity
 
  
// Several get functions, all of which are const members, ensure that the const object can also call bool isEmpty () const {return getSize () = 0;} int capacity () const {return theCapacity ;} int size () const {return theSize ;}
// Push and pop operations void push_back (T t) {if (theSize = theCapacity) reserve (theCapacity * 2 + 1); objects [theSize ++] = t ;} void pop_back () {theSize --;} T & back () {return objects [theSize-1];} const T & back () const {return objects [theSize-1];} // iterator typedef T * iterater; typedef const T * const_iterater; // Iin end and other operations iterater begin () {return objects;} const_iterater begin () const {return objects ;} iterater end () {return (objects + theSize);} const_iterater end () const {return (objects + theSize);} enum {SPACE_CAPACITY = 16}; private: T * objects; int theSize; int theCapacity ;};


 

Here, I will mention the const member function, also known as a regular member function or a read-only member function. The const object can only access the regular member function, and can only access the regular member function through the const pointer call, but there is a special case,Constructor and destructor are the only member functions that can be called by const objects but are not const member functions.

 

 

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.