First, the BEGIN function
Function Prototypes:
Iterator begin ();
Const_iterator begin ();
Function:
Returns an iterator to the starting element in the current vector container.
Second, End Function
Function Prototypes:
Iterator End ();
Const_iterator end ();
Function:
Returns an iterator to the end element in the current vector container.
Three, front function
Function Prototypes:
Reference Front ();
Const_reference Front ();
Function:
Returns a reference to the starting element in the current vector container.
Four, back function
Function Prototypes:
Reference back ();
Const_reference back ();
Function:
Returns a reference to the end element in the current vector container.
Instance:
#include <iostream>
#include < Vector>
using namespace std;
int main ()
{
vector<char> v1;
vector<char>::iterator Iter1;
vector<char>::iterator Iter2;
v1.push_back (' m ');
v1.push_back (' n ');
v1.push_back (' O ');
v1.push_back (' P ');
cout << "v1.front () =" << V1.front () << Endl;
cout << "v1.back () =" << v1.back () << Endl;
iter1 = V1.begin ();
cout << *iter1 << Endl;
iter2 = V1.end ()-1;//note v1.end () points to the next position of the last element, so access to the last element
// The correct operation is: V1.end ()-1;
cout << *iter2 << Endl;
return 0;
}
Output Result:
V1.front () = m
V1.back () = P
M
P
Use of the vector container begin () and end () function, front () and back () in C++stl