Beginner STL, the following content and when they accumulate, and then gradually improve.
Vector containers (vectors) are sequential containers, which are contiguous allocations of memory, support random access, and are very similar in terms of data arrangement.
The difference between an array and a vector is that the array is statically allocated space, and once the size of the space is allocated, it can no longer be changed, for example, int a[6]; vector is dynamically allocating memory, and as the element is constantly inserted, it expands its capacity in accordance with its own set of mechanisms. The capacity growth of the vector container is increased by one times the container's current capacity.
As for iterators, it resembles a pointer to an element in the vector.
------------------------------------------------------------------------
"Front function"
① function Prototypes:
Reference Front ();
Const_reference Front ();
② Features:
Returns a reference to the starting element in the current vector container, which returns the first element in the vector container.
------------------------------------------------------------------------
"Back function"
① function Prototypes:
Reference back ();
Const_reference back ();
② Features:
Returns a reference to the end element in the current vector container, which returns the last element in the vector container.
------------------------------------------------------------------------
The "Begin function"
① function Prototypes:
Iterator begin ();
Const_iterator begin ();
② Features:
Returns an iterator to the starting element in the current vector container.
------------------------------------------------------------------------
"End Function"
① function Prototypes:
Iterator End ();
Const_iterator end ();
② Features:
Returns an iterator to the end element in the current vector container.
------------------------------------------------------------------------
#include <iostream>#include<vector>using namespacestd;intmain () {vector<Char>v; Vector<Char>::iterator Iter1;//iterators 1vector<Char>::iterator Iter2;//iterators 2V.push_back ('A'); V.push_back ('B'); V.push_back ('C'); V.push_back ('D'); V.push_back ('E'); cout<<"v.front () ="<<v.front () <<endl;//Output The first element in a vector containercout<<"v.back () ="<<v.back () <<endl;//Output The last element in a vector containerIter1=V.begin (); cout<<*iter1<<Endl; Iter2= V.end ()-1;//Note that v.end () points to the next position of the last element, so the last element is accessed//the correct operation is: V1.end ()-1cout << *iter2 <<Endl;return 0;}
OUTPUT:
V.front () = A
V.back () = E
A
E
Vector container of C++stl