Use of the vector container begin () and end () function, front () and back () in C++stl

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.