vector rbegin() and rend() function in C++ STL
- vector::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container.
Syntax:
vector_name.rbegin()
Parameters: The function does not accept any parameter.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.Return value: The function returns a reverse iterator pointing to the last element in the container.
Program to demonstrate the vector::rbegin() method:
Program 1:
// CPP program to illustrate// the vector::rbegin() function#include <bits/stdc++.h>usingnamespacestd;intmain(){vector<int> v;v.push_back(11);v.push_back(12);v.push_back(13);v.push_back(14);v.push_back(15);// prints all the elementscout <<"The vector elements in reverse order are:\n";for(autoit = v.rbegin(); it != v.rend(); it++)cout << *it <<" ";return0;}Output:The vector elements in reverse order are: 15 14 13 12 11
- vector::rend() is a built-in function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first element in the array container.
Syntax:
vector_name.rend()
Parameters: The function does not take any parameter.
Return value: The function returns a reverse iterator pointing to the theoretical element right before the first element in the array container.
Program to demonstrate the vector::rend() method:
Program 1:
// CPP program to illustrate// the vector::rend() function#include <bits/stdc++.h>usingnamespacestd;intmain(){vector<int> v;v.push_back(11);v.push_back(12);v.push_back(13);v.push_back(14);v.push_back(15);cout <<"The last element is: "<< *v.rbegin();// prints all the elementscout <<"\nThe vector elements in reverse order are:\n";for(autoit = v.rbegin(); it != v.rend(); it++)cout << *it <<" ";return0;}Output:The last element is: 15 The vector elements in reverse order are: 15 14 13 12 11

