Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 13 Jun, 2022
  1. list::rbegin() is an inbuilt function in C++ STL that returns a reverse iterator which points to the last element of the list. Syntax:
list_name.rbegin()
  1. Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the last element of the list. Below program illustrates the above function: 

CPP




// C++ program to illustrate the
// list::rbegin() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 list<int> lis = { 10, 20, 30, 40, 50 };
 
 cout << "The list in reverse order: ";
 
 for (auto it = lis.rbegin(); it != lis.rend(); ++it)
  cout << *it << " ";
 
 return 0;
}

Output:

The list in reverse order: 50 40 30 20 10

Time Complexity: O(n)

Auxiliary Space: O(1)

  1. list::rend() is an inbuilt function in C++ STL that returns a reverse iterator which points to the position before the beginning of the list. Syntax:
list_name.rend()
  1. Parameter: The function does not accept any parameters. Return value: It returns a reverse iterator which points to the position before the beginning of the list. Below program illustrates the above function: 

CPP




// C++ program to illustrate the
// list::rbegin() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 list<int> lis = { 10, 20, 30, 40, 50 };
 
 cout << "The list in reverse order: ";
 
 for (auto it = lis.rbegin(); it != lis.rend(); ++it)
  cout << *it << " ";
 
 return 0;
}

Output:

The list in reverse order: 50 40 30 20 10

Time Complexity: O(n)

Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!