Vector Summary of STL

Source: Internet
Author: User
0 -- Internal Type Definition
Definition of vector in the standard module: Template <class T, class alloc = alloc> class vector {......};

 

Typedef t value_type; Element type of Vector  
Typedef value_type * pointer; Pointer type  
Typedef const value_type * const_pointer; Const pointer type  
Typedef value_type * iterator; Iterator type  
Typedef const value_type * const_iterator; Const iterator type  
Typedef value_type & reference; Reference Type  
Typedef const value_type & const_reference; Const reference type  
Typedef size_t size_type; Numeric type used for vector subscript index and capacity  
Typedef ptrdiff_t difference_type; Used as a numerical value for the location gap between two elements of a vector.  
1 -- Constructor
Vector <t> ()

Construct an empty vector instance without any content

Vector <t> (const vector <t> & X)

Copy the constructor and use instance X for initialization.

Vector <t> (size_type N)

Construct a vector instance and use the variable initialization of the Instance constructed by n t ()

Vector <t> (size_type N, const T & value)

Use a vector instance and initialize it with n t-type instance data

Vector <t> (inputiterator begin, inputiterator end)

Construct a vector instance and initialize it with the element specified by the iterator [begin, end ).

2 -- Value assignment function
Vector <t> & operator = (const vector <t> & X)

Assign a value to the current variable.

Void assign (size_type N, const T & Val)

Replace existing elements with n t-type instances

Void assign (inputiterator begin, inputiterator end)

Replace the existing element with the element specified by the iterator [begin, end ).

3 -- Element access function (iterator, reference and access method)
Reference at (size_type N)

Returns the reference of the nth element.

Const_reference at (size_type N)

Returns the const reference of the nth element.

Reference operator [] (size_type N)

Returns the reference of the nth element.

Const_reference operator [] (size_type N)

Returns the const reference of the nth element.

Reference Front ()

Returns the reference of the first element. (If the vector is empty, coredump is triggered)

Reference back ()

Returns the reference of the last element. (If the vector is empty, coredump is triggered)

Const_reference Front ()

Returns the const reference of the first element. (If the vector is empty, coredump is triggered)

Const_reference back () const

Returns the const reference of the last element. (If the vector is empty, coredump is triggered)

Iterator begin ()

The returned iterator points to the first element of the vector.

Iterator end ()

The returned iterator points to the "next to the end element" of the vector ".
It is usually called the off-the-end iterator, indicating that it points to a nonexistent element. If the vector is empty, the iterator returned by begin is the same as the iterator returned by end.

Const_iterator begin ()

Returns the const iterator pointing to the first element of the vector.

Const_iterator begin ()

Returns the const iterator pointing to the "next to the end element" of the vector"

Reverse_iterator rbegin ()

Returns the reverse iterator pointing to the last element of the vector.

Reverse_iterator rend ()

Returns the reverse iterator pointing to the first position of the first element of the vector.

Const_reverse_iterator rbegin () const

Returns the const reverse iterator pointing to the last element of the vector.

Cosnt_reverse_iterator rend ()

Returns the const reverse iterator pointing to the previous position of the first element of the vector.

4 -- add element Functions
Void push_back (const T & X)

Generate a new T type data, call the copy constructor of t to initialize the new variable with X as the template, and put it at the end of the vector.

Iterator insert (iterator POs, const value_type & X)

Insert element X at the POs position of the vector. Returns the iterator for inserting elements.

Void insert (iterator POs, size_type N, const value_type & X)

Insert n elements X at the beginning of the POs position of the vector.

Void insert (iterator POs, inputiterator begin, inputiterator end)

Insert the [begin, end) element at the beginning of the POs position of the vector.

Inline void swap (vector <t> & X, vector <t> & Y)

A non-member function is used to exchange elements of two vectors.

Void reserve (size_type N)

Expand if the capacity is insufficient to accommodate n elements. (This operation may modify the vector, resulting in failure of all previous iterators and references)

Void resize (size_type N)

Change the number of elements to n. If the vector increases, the new elements must be constructed using the default constructor of the element type.

Void resize (size_type N, value_type X );

Change the number of elements to n. If the vector increases, all the extra elements are copies of X.

5 -- delete a function
Void clear ()

Delete all elements in the vector and clear the container.

Void pop_back ()

Deletes the last element of a vector.

Iterator erase (iterator POS)

Delete the element pointed to by the iterator POs and return the iterator or end () of the last element in the deleted vector ().

Iterator erase (iterator begin, iterator end)

Delete the element referred to by the iterator [begin, end), and return the iterator or end () of the last element in the deleted vector of the original range element ().

6 -- Comparison Functions
Inline bool operator = (const vector <t> & LS, const vector <t> & RS)

A non-member function is used to determine whether two vectors are equal. First, compare whether the number of elements is the same. If the number is the same, traverse the vector and call the "=" operator of the element to compare whether the number is the same.

Inline bool Operator! = (Const vector <t> & LS, const vector <t> & RS)

A non-member function is used to determine whether two vectors are unequal. The result is the inverse of the "=" operator.

Inline bool operator <(const vector <t> & LS, const vector <t> & RS)

A non-member function is used to determine whether the former is smaller than the latter. Traverse the vector and return the comparison result of the first pair of elements that do not meet the "=" operator.

Inline bool operator> (const vector <t> & LS, const vector <t> & RS)

A non-member function is used to determine whether the former is greater than the latter. Traverse the vector and return the comparison result of ">" for the first element that does not satisfy the "=" operator.

Inline bool operator <= (const vector <t> & LS, const vector <t> & RS)

A non-member function is used to determine whether the former is "<=" or not. The result is "! (RS <ls )".

Inline bool operator >=( const vector <t> & LS, const vector <t> & RS)

A non-member function is used to determine whether the former is "> =. The result is "! (LS <RS )".

7 -- element capacity Function
Bool empty () const

Determines whether the current vector is empty.

Size_type size () const

Obtains the number of elements contained in the current vector.

Size_type max_size () const

Returns the maximum number of elements that a vector can contain.

Size_type capacity () const

Obtains the maximum number of elements that can be accommodated without allocating memories.

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.