A vector is a dynamic array. STL source code:
// Myvector. h # include "mymemory. H "# include" myiterator. H "# include" myconstruct. H "Using STD: copy_backward; Using STD: Max; Template <class T, class alloc = alloc> class vector {public: typedef t value_type; // after this sentence is defined, so all types are related to value_type rather than T typedef value_type * pointer; typedef value_type * iterator; typedef value_type & reference; typedef size_t size_type; typedef ptrdiff_t defference_type; protected: // define your own configurator typedef s Imple_alloc <value_type, alloc> data_allocator; iterator start; // use the space header iterator finish; // use the space's end iterator end_of_storage; // void insert_aux (iterator position, const T & X); void deallocate () // wrap the configuration function {If (start) data_allocator :: deallocate (START, end_of_storage-start);} void fill_initialize (size_type N, const T & Value) {start = allocate_and_fill (n, value); finish = start + N; end_of_storage = finish ;} public: iterator B Egin () {return start;} iterator end () {return finish;} size_type size () const {return size_type (finish-Start);} size_type capacity () const {return size_type (end_of_storage-begin ();} size_type empty () const {return begin () = end ();} reference operator [] (size_type N) {return * (begin () + n);} vector (): Start (0), finish (0), end_of_storage (0) {} vector (size_type N, const T & Value) {fill_initialize (n, value);} explicit VEC Tor (size_type N) {fill_initialize (n, T ());}~ Vector () {destroy (START, finish); deallocate () ;}reference Front () {return * begin () ;} reference back () {return * (end () -1);} void push_back (const T & X) {If (finish! = End_of_storage) {construct (finish, x); // global function ++ finish;} elseinsert_aux (end (), x); // new space will be configured} void pop_back () {-- finish; destroy (finish) ;}// starting from position, n elements are inserted. The initial values of these elements are xvoid insert (iterator position, size_type N, const T & X ); // insert xvoid insert (iterator position, const T & X) {insert (Position, 1, x);} iterator erase (iterator position) at position) {// clear the element if (Position + 1! = End () Copy (Position + 1, finish, position); -- finish; destroy (finish); Return position;} iterator erase (iterator first, iterator last ); void resize (size_type new_size, const T & X) {If (new_size <size () Erase (begin () + new_size, end (); elseinsert (end (), new_size-size (), x);} void resize (size_type new_size) {resize (new_size, T ();} void clear () {erase (begin (), end ();} protected: iterator allocate_and_fill (size_type N, const T & X) {iterator result = data_allocator: allocate (n); uninitialized_fill_n (result, n, x); return result ;}; // define template <typename t, class alloc> void vector <t, alloc>: insert_aux (iterator position, const T & X) {If (finish! = End_of_storage) {// there is also a space construct (finish, * (finish-1); // In the backup space, divide the content into an element, the value is + + finish; t x_copy = x; copy_backward (Position, finish-2, finish-1); * position = x_copy ;} else {const size_type old_size = size (); const size_type Len = (old_size! = 0 )? 2 * old_size: 1; // if the original size is 0, the new length is 1 iterator new_start = data_allocator: allocate (LEN); iterator new_finish = new_start; try {// copy the original vector to the new vectornew_finish = uninitialized_copy (START, position, new_start); // set the initial value construct (new_finish, x) for the new element; ++ new_finish; new_finish = uninitialized_copy (Position, finish, new_finish);} catch (...) {// commit or rollbackdestroy (new_start, new_finish); data_allocator: deallocate (new_start, Len); th Row;} // destructor, releasing the original vectordestroy (begin (), end (); deallocate (); Start = new_start; finish = new_finish; end_of_storage = new_start + Len ;}// template <typename T, typename alloc> typename vector <t, alloc >:: iterator vector <t, alloc> :: erase (iterator first, iterator last) {iterator I = copy (last, finish, first); destroy (I, finish); finish = finish-(last-fir St); return first;} // ---------------------------------------------------------------------------- // This code is very poorly written because it can be successfully moved once when data is moved, // you have to move the template <typename T, typename alloc> void vector <t, alloc>: insert (iterator position, size_type N, const T & X) multiple times in segments) {If (n! = 0) {If (size_type (end_of_storage-finish)> = N) {// spare space t x_copy = x; const size_type elems_after = finish-position; iterator old_finish = finish; if (elems_after> N) {// The number of existing elements after the insertion point is greater than the number of new elements uninitialized_copy (finish-N, finish, finish); finish + = N; copy_backward (position, old_finish-n, old_finish); fill (Position, Position + N, x_copy);} else {// The number of existing elements after the insertion point is less than or equal to the number of new elements uninitialized_fill_n (finish, n-elems_after, x_copy); finish + = n-elems_after; uninitialized_copy (Position, old_finish, finish); finish + = elems_after; fill (Position, old_finish, x_copy );}} else {// The standby space is smaller than the number of new elements. Const size_type old_size = size (); const size_type Len = old_size + max (old_size, n); iterator new_start = data_allocator :: allocate (LEN); iterator new_finish = new_start; try {new_finish = uninitialized_copy (START, position, new_start); new_finish = begin (new_finish, n, x); new_finish = uninitialized_copy (position, finish, new_finish);} catch (...) {// commit and rollbackdestroy (new_start, new_finish); data_allocator: deallocate (new_start, Len); throw;} destroy (START, finish); deallocate (); // The repackaged function start = new_start; finish = new_finish; end_of_storage = new_start + Len ;}}// restart ;}}}//---------------------------------------------------------------------------