C++ vector::begin() with example

In C++, the standard library provides us with vectors, which are dynamic arrays. It consists of multiple homogeneous objects, which can be accessed by their position in the vector. It is automatically resized (if needed). The vector::begin() is a C++ Standard Library function which returns an iterator pointing to the first element in the vector.



Parameter: The vector::begin() function does not accept any parameter.
Return value: The vector::begin() function returns an iterator to the first element.

Example of vector::begin() function

#include <iostream>
#include <vector>
using namespace std;

int main ()
  vector<int> vec;

  for(int i=1;i<=5;i++)

  cout<<"Vector: ";
  for(vector<int>::iterator it=vec.begin();it!=vec.end();++it)
    cout<<*it<<"  ";

  vector<int>::iterator it=vec.begin();
  cout<<"Value pointed by offset 2 is: "<< *(it+2);



Vector: 1  2  3  4  5
Value pointed by offset 2 is: 3

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]