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

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of list::rbegin() and list::rend() functions in C++ STL.

What is a List in STL?

List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List performs better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is like forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is the list::rbegin()?

list::rbegin() is an inbuilt function in C++ STL which is declared in header file. rbegin() is a reverse begin function. rebegin() returns a reverse iterator which is pointing to the last element of the list. Reverse iterator is an iterator which moves in reverse direction, starting from the end and will move towards the start. However back() also returns the last element but unlike the simple iterator this bidirectional iterator moves in backward direction.

Syntax

list_container1.rbegin();

Parameter

This function accepts no parameters.

Example

Input: list<int> List_container = {10, 11, 13, 15};
      List_container.rbegin();
Output:
      List= 15

Return Value

This function returns a reverse iterator pointing to the last element of the list. Reverse iterator is an iterator which moves in backward direction.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   list<int> myList = { 10, 20, 30, 40 };
   cout<<"List is: ";
   for (auto i = myList.rbegin(); i!= myList.rend(); ++i)
      cout << *i << " ";
   return 0;
}

Output

If we run the above code it will generate the following output −

List is: 40 30 20 10

What is the list::rend()?

list::rend() is an built-in function in C++ STL which is declared in header file. rend() is a reverse end function. rend() returns a reverse iterator which is pointing to the position before the first element of the list container associated. Reverse iterator is an iterator which moves in reverse direction, starting from the end and will move towards the start. However back() also returns the last element but unlike the simple iterator this bidirectional iterator moves in backward direction.

Syntax

list_container1.rend();

This function accepts no parameters.

Example

Input: list<int> List_container= { 10, 11, 13, 15};
      List_container.rend();
Output:
      List= 5 //will display random value which is before the beginning of the list

Return Value

This function returns a reverse iterator which is pointing to the element before the first element in the list. Reverse iterator is an iterator which moves in backward direction.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   list<int> myList = { 10, 20, 30, 40 };
      cout<<"List is : ";
   for (auto i = myList.rbegin(); i!= myList.rend(); ++i)
      cout << *i << " ";
   return 0;
}

Output

If we run the above code it will generate the following output −

List is: 40 30 20 10
raja
Published on 06-Mar-2020 07:23:36
Advertisements