Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of vector container begin () and end () function, front () and back () in C++STL

Shulou Source: shulou.com Published: 2022-06-03 05:44:17 09月22日 Update

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

Tags: Function container element function prototype allocation capacity array continuous memory content method more end space problem growth start iteration similar Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi macOS Microsoft Xiaomi Huawei