What is brought to you today is an STL container, an array-like C + + container--vector.
Enter the topic:
What is a vector.
Vector is a single-port container, vector refers to the content is a continuous space, support random access, in addition, vector container space is dynamic growth. The vector function is basically as shown in Figure 1-1.
Figure 1-1
Note:
The so-called dynamic increase in size, not in the original space after the subsequent access to new space (because there is no guarantee of space after the original space), but a larger memory space, and then copy the original data new space, and release the original space. Therefore, any operation on the vector, once the space is reconfigured, all iterators pointing to the original vector are invalidated. This is a mistake that programmers easily make, be careful. |
The following is a common API for vector containers
Vector Constructor
Vector<t> v; Implement class implementation with template, default constructor
Vector (V.begin (), V.end ());//Copies the elements of the V[begin (), End ()) interval to itself.
A vector (size_type_count,constvalue_type&_val)//constructor copies _count _val to itself.
Vector (const VECTOR&VEC);//copy constructor.
Example using the second constructor we can ...
int arr[]= {2,3,4,1,9};
Vector<int>v1 (arr, arr + sizeof (arr)/sizeof (int));
Vector Common assignment Operation
Assign (beg, end);//assigns a copy of the data in the [Beg, end) interval to itself.
Assign (n, elem);//assigns a copy of n elem to itself.
vector& operator= (const vector &VEC);//overloaded equals operator
Swap (VEC);//Swap the VEC with its own elements.
swap extra function: Compressed space
Vector<int> (v). Swap (v);
Vector size Operation
Size ();//Returns the number of elements in the container
Empty ();//Determine if the container is null
Resize (int num);//Reassign the container's length to num, and if the container becomes longer, the new position is populated with the default value. If the container is shorter, the element at the end of the container length is removed.
Resize (int num,elem);//re-specify the container's length to num, and if the container is longer, fill the new position with the Elem value. If the container is shorter, the elements at the end of the container length > are removed.
Capacity ();//capacity of the container
Reserve (int len);//The container reserves Len element length, the reserved position is not initialized, and the element is inaccessible.
vector data Access Operations
at (int idx); Returns the data that index IDX refers to, if the IDX is out of bounds, throws a Out_of_range exception.
operator[];//returns the data indicated by the index IDX, and runs a direct error when it crosses the border
Front ();//Returns the first Data element in a container
Back ();//Returns the last data element in the container
vector Insert and delete operations
Insert (const_iterator pos, int count,ele);//iterators point to position pos to insert count of elements ele.
Push_back (ele); Trailing Insert element ele
Pop_back ();//Delete last element
Erase (const_iterator start, const_iterator end);//delete the element between the iterator from start to end
Erase (const_iterator POS);//delete the element that the iterator points to
Clear ();//delete all elements in the container