Vector Principle and usage

 

First, what is the vector?

Vector (the Vector) is a dynamic sequential container encapsulates the array size (Sequence Container). Like any other type of container, it is possible to store various types of objects. Simply believed that the vector is a dynamic array can store any type.
Second, the characteristics of the container
1. The order of the sequence
order of the container elements according to an order strictly linear. The corresponding elements can be accessed by the position of elements in the sequence.
2. Dynamic Array
support for arbitrary element of a sequence for quick direct access, even to carry out the operation described by the pointer operator. Operation for relatively quickly add / delete elements in the end of the sequence.
3. Since the memory space is continuous, in the middle will be deleted and inserted in the copied memory block; when the memory space is not enough, you need to apply a large enough memory and memory copy.
Third, the basic function implementation
1. constructor
vector (): Create an empty Vector
Vector (int nSize): Create a Vector, the number of elements nSize
Vector (int nSize, const T & T): Create a Vector, is the number of elements nSize, and the values are T
vector (vector const &): copy constructor
vector (begin, end): copying the elements of another array [begin, end) into the vector section
2. increasing function
void push_back (const T & x) : vector tail add an element X-
iterator INSERT (iterator IT, const T & x): vector iterator add an element x to point the front element
iterator insert (iterator it, int n , const T & x): vector iterator pointing element before increasing n identical elements X
iterator iNSERT (IT iterator, a const_iterator First, a const_iterator Last): data between the same type into another vector iterator to the front element vector [First, Last)
3. delete function
iterator erase (iterator it): delete vector iterator to the element
iterator erase (iterator first, iterator last ): delete element vector [first, last) in the
void pop_back (): delete the last element in the vector
void clear (): Clear all vector elements
4. Loop function
reference at (int pos): Returns the reference position of the element pos
reference front (): returns a reference to the first element
reference back (): returns a reference to the last element
iterator begin ( ): returns a vector of the first pointer to the first element
iterator end (): returns the vector tail pointer points to the next position of the last element vector
reverse_iterator rbegin (): reverse iterator pointing to the last element
reverse_iterator rend (): reverse iterator position before the first element
5. Analyzing function
bool empty () const: determining whether the empty vector, if it is empty, no element of the vector
6. the size function
int size () const: returns vector the number of elements
int capacity () const: returns the current maximum value of the element can be accommodated in the vector Zhang
int max_size () const: returns the maximum number of allowable values of vector elements
7. other functions
void swap (vector &): exchange two a vector of the same type of data
void assign (int n, const T & x): n-th set vector element value X
void ASSIGN (a const_iterator first, a const_iterator Last): vector [first, last) Ghost Set to the current vector element
8. looked clear
1.push_back added last in a data array
2.pop_back remove the last of the data array
3.at number position data obtained
4.begin obtained pointer array head
pointer of the last unit + 5.end resulting array
6. REFERENCE front head array obtained
7.back array obtained last reference unit
8.max_size maximum vector may be obtained much
9.capacity vector allocated size of the current
10.size size of data currently used
11.resize change the current usage data size, if large, than those that use current fill default
size 12.reserve changing the current space allocated vecotr
13.erase pointer to the deleted items
14.clear emptied current vector
15.rbegin will reverse the vector start returns a pointer (actually the original. 1-end)
16.rend the inverted configuration of a vector return end pointer (actually the original. 1-the begin)
17.empty determines whether the vector is empty
18.swap exchange data with another vector

Guess you like

Origin www.cnblogs.com/dingou/p/10299723.html