vector rbegin() and rend() function in C++ STL

09 Jun, 2022
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::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.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:  
Parameters: The function does not accept any parameter.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
// CPP program to illustrate// the vector::rbegin() function#include <bits/stdc++.h>using namespace std; int main(){    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 elements    cout << "The vector elements in reverse order are:\n";    for (auto it = v.rbegin(); it != v.rend(); it++)        cout << *it << " ";    return 0;}
 
 
The vector elements in reverse order are:
15 14 13 12 11
 
 Time Complexity – constant O(1)
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 vector container.Syntax:  
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 vector 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 vector container.Program to demonstrate the vector::rend() method: Program 1:  
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 vector container.Program to demonstrate the vector::rend() method: Program 1:  
CPP
// CPP program to illustrate// the vector::rend() function#include <bits/stdc++.h>using namespace std; int main(){    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 elements    cout << "\nThe vector elements in reverse order are:\n";    for (auto it = v.rbegin(); it != v.rend(); it++)        cout << *it << " ";    return 0;}
 
 
The last element is: 15
The vector elements in reverse order are:
15 14 13 12 11
 
 Time Complexity – constant O(1)
abhijeetmurmu1997
utkarshgupta110092
CPP-Functions
cpp-vector
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Pair in C++ Standard Template Library (STL)
Friend class and function in C++
std::string class in C++
std::find in C++
Queue in C++ Standard Template Library (STL)
Unordered Sets in C++ Standard Template Library
List in C++ Standard Template Library (STL)
Power Function in C/C++