What is the use of vector container begin () and end () function, front () and back () in C++STL
What is the use of vector container begin () and end () function, front () and back () in C++STL? I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Vector container (vector) is a sequential container, which is a continuously allocated memory and supports random access. From the point of view of data arrangement, it is very similar to an array. The difference between an array and vector is that an array is a statically allocated space. Once the size of the space is allocated, it cannot be changed. For example, int a [6] While vector allocates memory dynamically, with the continuous insertion of elements, it will continue to expand its capacity according to its own set of mechanisms. The capacity growth of vector containers is double the current capacity of containers.
Begin function:
Function prototype:
Iterator begin ()
Const_iterator begin ()
Features:
Returns an iterator of the starting element in the current vector container.
End function:
Function prototype:
Iterator end ()
Const_iterator end ()
Features:
Returns an iterator for the last element in the current vector container.
Front function:
Function prototype:
Reference front ()
Const_reference front ()
Features:
Returns a reference to the starting element in the current vector container.
Back function:
Function prototype:
Reference back ()
Const_reference back ()
Features:
Returns a reference to the last element in the current vector container.
Example:
# include
# include
Using namespace std
Int main ()
{
Vector v1
Vector::iterator iter1
Vector::iterator iter2
V1.push_back ('m')
V1.push_back ('n')
V1.push_back ('o')
V1.push_back ('p')
Cout