C++ vector::rbegin() usage 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::rbegin() is a C++ Standard Library function which returns a reverse iterator pointing to the last element in the vector.

Adding an offset to a reverse iterator moves it in the opposite direction i.e. towards the left
Subtracting an offset moves it towards the right



Parameter: The vector::rbegin() function does not accept any parameter.
Return value: The vector::rbegin() function returns a reverse iterator to the last element.

Example of vector::rbegin() 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>::reverse_iterator it=vec.rbegin();
  cout<<"Value pointed by reverse iterator is: "<< *it<<endl;
  cout<<"Value pointed by offset 3 is: "<< *(it+3);



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

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"]