C++ List::rbegin() Function



The C++ std::list::rbegin() function is used to retrieve a reverse iterator to the first element of the reversed list. It correspond to the last element of the non-reversed list.

In C++, an iterator is an object are used to point at the memory addresses of STL(standard template library) containers. We use the (*) symbol to retrieve an element of the current position pointed by the iterator. The (*) symbol is known as the asterisk operator in C++.

If the list is an empty, the returned iterator is equal to the rend().

Syntax

Following is the syntax of the C++ std::list::rbegin() function −

reverse_iterator rbegin();

Parameters

  • It does not accept any parameter.

Return Value

This function returns a reverse iterator.

Example 1

In the following program, we are using the C++ std::list::rbegin() function to retrieve a reverse iterator to the first element of the current reversed list {10, 20, 30, 40, 50}.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<int> num_list = {10, 20, 30, 40, 50};
   cout<<"List elements are: ";
   for(int n : num_list) {
      cout<<n<<" ";
   }
   auto ri = num_list.rbegin();
   cout<<"\nThe iterator of the last element: ";
   cout<<*ri;
}

Output

Following is the output of the above program −

List elements are: 10 20 30 40 50 
The iterator of the last element: 50

Example 2

Apart from the int-type list element, you can also retrieve an iterator of any other type list element like char and string list content.

Following is another example of the C++ std::list::rbegin() function. Here, we are creating a list(type char) named vowels with the values {'a', 'e', 'i', 'o', 'u'}. Then using the rbegin() function, we are trying to retrieve a reverse iterator to the first element of the current reversed list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<char> vowels = {'a', 'e', 'i', 'o', 'u'};
   cout<<"List elements are: ";
   for(char v : vowels) {
      cout<<v<<" ";
   }
   auto ri = vowels.rbegin();
   cout<<"\nThe iterator of the last element: ";
   cout<<*ri;
}

Output

This will generate the following output −

List elements are: a e i o u 
The iterator of the last element: u

Example 3

If the list type is a string.

In this example, we are creating a list(type string) named fname with the values {"Rohan", "Reema", "Geeta", "Seema"}. Then, using the rbegin() function, we are trying to retrieve a reverse iterator to the first element of the current reversed list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<string> names = {"Rohan", "Reema", "Geeta", "Seema"};
   cout<<"List elements are: ";
   for(string s : names) {
      cout<<s<<" ";
   }
   auto ri = names.rbegin();
   cout<<"\nThe iterator of the last element: ";
   cout<<*ri;
}

Output

The above program produces the following output −

List elements are: Rohan Reema Geeta Seema 
The iterator of the last element: Seema

Example 4

Using for loop along with the rbegin() function to retrieve the iterator of the container element.

In this example, we use the rbegin() function along with the for loop to loop through the current container {1, 2, 3, ,4 ,5} and retrieve an iterator of all elements in the container in reverse order.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<int> num_list = {1, 2, 3, 4 ,5};
   cout<<"List elements are: ";
   for(int n : num_list) {
      cout<<n<<" ";
   }
   cout<<"\nA reverse iterator of an elements are : ";
   for(auto i = num_list.rbegin(); i!=num_list.rend(); i++) {
      cout<<*i<<" ";
   }
}

Output

On executing the above program, it will produce the following output −

List elements are: 1 2 3 4 5 
A reverse iterator of an elements are : 5 4 3 2 1 
Advertisements