C++ List Library - crbegin() Function



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

The crbegin() function stands for constant reverse begin. 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++.

Syntax

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

reverse_iterator crbegin() const;

Parameters

  • It does not accept any parameter.

Return Value

This function returns constant reverse iterator.

Example 1

In the following program, we are using the C++ std::list::crbegin() function to retrieve a constant 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.crbegin();
   cout<<"\nThe constant reverse 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 constant reverse 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::crbegin() function. Here, we are creating a list(type char) named vowels with the values {'a', 'e', 'i', 'o', 'u'}. Then using the crbegin() function, we are trying to retrieve the constant reverse iterator to the first element of the 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.crbegin();
   cout<<"\nThe constant reverse iterator of the last element: ";
   cout<<*ri;
}

Output

This will generate the following output −

List elements are: a e i o u 
The constant reverse 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 crbegin() function, we are trying to retrieve a constant 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.crbegin();
   cout<<"\nThe constant reverse iterator of the last element: ";
   cout<<*ri;
}

Output

The above program produces the following output −

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

Example 4

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

In this example, we use the crbegin() function along with the for loop to loop through the current container {1, 2, 3, ,4 ,5} and retrieve a constant 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 constant reverse iterator of an elements are : ";
   for(auto i = num_list.crbegin(); 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 constant reverse iterator of an elements are : 5 4 3 2 1
Advertisements