C ++ STL vector Implementation (2)

Source: Internet
Author: User

In the previous article, we discussed the vector declaration. Next, you will see the vector interface and implementation.

Class vector {

Public:

...

Iterator begin () {return start;} // return the beginning of vetor
Const_iterator begin () const {return start;} // read-only access
Iterator end () {return finish;} // return the end of the Vector
Const_iterator end () const {return finish;} // read-only access
Reverse_iterator rbegin () {return reverse_iterator (end ();} // reverse iterator
Const_reverse_iterator rbegin () const {
Return const_reverse_iterator (end ());
} // Read-only access
Reverse_iterator rend () {return reverse_iterator (begin ();} // reverse iterator
Const_reverse_iterator rend () const {
Return const_reverse_iterator (begin ());
}
Size_type size () const {return size_type (end ()-begin ();} // size, the length of the Vector
Size_type max_size () const {return size_type (-1);} // The maximum length supported by the vector, 4294967295
Size_type capacity () const {return size_type (end_of_storage-begin ();} // the current capacity of the vector. It indicates the capacity of the current vector.
Bool empty () const {return begin () = end () ;}// determines whether vetor is null, so that this function is always a constant time complexity. Judge whether the vector is space-used empty. Do not use size () = 0, because size () may be linear time complexity.
Reference operator [] (size_type N) {return * (begin () + n);} // [] Operator
Const_reference operator [] (size_type N) const {return * (begin () + n);} // read-only

Vector (): Start (0), finish (0), end_of_storage (0) {}// default constructor

Vector (size_type N, const T & Value) {// The initialization length is N, and each value is the vetor of value.
Start = data_allocator: allocate (n); // The Memory Allocation of the vector is delegated to allocator.
Iterator first = start;

While (n --)

{

* First ++ = value;

}
Finish = start + N;
End_of_storage = finish;
}

Vector (size_type N) {// The initial length is N, and each value is the default Vector
Start = data_allocator: allocate (N );
While (n --)

{

* First ++ = T ();

}

Finish = start + N;
End_of_storage = finish;
}

Vector (const vector <t/*, alloc */> & X) {// copy the constructor
Start = data_allocator: allocate (X. End ()-X. Begin ());

Finish = start;

For (iterator I = x. Begin (); I! = X. End (); ++ I)

{

* Finish ++ = * I;

}
End_of_storage = finish;
}


Vector (const_iterator first, const_iterator last) {// iterator Constructor
Size_type n = 0;
Distance (first, last, n); // judge the distance from first to last
Start = data_allocator: allocate (N );
Finish = start;

For (iterator I = x. Begin (); I! = X. End (); ++ I)

{

* Finish ++ = * I;

}
End_of_storage = finish;
}

~ Vector () {// destructor of Vector
Destroy (START, finish); // call the destructor of each element
Deallocate (); // release the memory of the Vector
}
Vector <t/*, alloc */> & operator = (const vector <t/*, alloc */> & X );
Void reserve (size_type N) {// The reverse function of the vector, mainly used for vector resizing, increasing the vector capacity to the size specified by N. To prevent excessive insertion and copying of a vector, we recommend that you use this function to apply a large enough capacity for the vector.
If (capacity () <n ){
Const size_type old_size = size ();
Const iterator TMP = data_allocator: allocate (N );
Uninitialized_copy (begin (), end (), TMP );
Destroy (START, finish );
Deallocate ();
Start = TMP;
Finish = TMP + old_size;
End_of_storage = start + N;
}
}
Reference Front () {return * begin ();} // returns the first element of the vector.
Const_reference Front () const {return * begin ();}//
Reference back () {return * (end ()-1);} // returns the last element of the vector.
Const_reference back () const {return * (end ()-1 );}
Void push_back (const T & X) {// push_back of the vector, the most common function, to add value at the end of the Vector
If (finish! = End_of_storage ){
* Finish = X;
++ Finish;
} Else
Insert_aux (end (), x); // insert if the vector capacity is full.
}
Void swap (vector <t/*, alloc */> & X) {// exchange the values of two vectors. This is very efficient. Constant time exchange.
: Swap (START, X. Start );
: Swap (finish, X. Finish );
: Swap (end_of_storage, X. end_of_storage );
}


Iterator insert (iterator position, const T & X) {// insert function of Vector
Size_type n = position-begin ();
If (finish! = End_of_storage & position = end ()){
* Finish = * position;
++ Finish;
} Else
Insert_aux (Position, x); // insert a vector to copy and adjust the position of the element. Therefore, copying is required, which is less efficient.
Return begin () + N;
}
Iterator insert (iterator position) {return insert (Position, T ());}
Void insert (iterator position, const_iterator first,
Const_iterator last );
Void insert (iterator position, size_type N, const T & X );
Void pop_back () {the last element is displayed.
-- Finish;
Destroy (finish );
}
Void erase (iterator position) {// Delete the element of the vector. deleting the last value is a constant time. Otherwise, you need to adjust the element and copy it again, which is inefficient.
If (Position + 1! = End ())
Copy (Position + 1, end (), position );
-- Finish;
Destroy (finish );
}
Void erase (iterator first, iterator last ){
Vector <t/*, alloc */>: iterator I = copy (last, end (), first); // needs to be copied again
Destroy (I, finish); // Delete the location to be deleted
Finish = finish-(last-first );
}
Void resize (size_type new_size, const T & X) {// adjust the length of the Vector
If (new_size <size ())
Erase (begin () + new_size, end ());
Else
Insert (end (), new_size-size (), X );
}
Void resize (size_type new_size) {resize (new_size, T ());}
Void clear () {erase (begin (), end () ;}// clear all elements of the vector to ensure linear time, O (n ).

}

 

Let's talk about the common interfaces of vector.

For more code, go to www.sgi.com/stl.

 

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.