C++ List::crend() Function



The C++ std::list::crend() function is used to retrieve a constant reverse iterator to the element following the last element of the reversed list.

The const_reverse_iterator is an iterator that iterates in the backward direction while pointing to a constant iterator. A const_reverse_iterator value determines where it starts in the list container and where it ends. However, even if the list element is not constant, it cannot be used to modify the contents it points to.

Syntax

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

const_reverse_iterator crend() const;

Parameters

  • It does not accept any parameter.

Return Value

This function returns a constant reverse iterator to the element following the last element of the reversed list.

Example 1

In the following program, we are using the C++ std::list::crend() function to retrieve a constant iterator to the element following the last element of the current list {10, 20, 30, 40}.

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

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

Output

Following is the output of the above program −

List elements are: 10 20 30 40 
A constant reverse iterator of the last element: 4

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::crend() function. Here, we are creating a list(type char) named char_list with the values {'a', 'b', 'c', 'd', 'e', 'f'}. Then using the crend() function, we are trying to retrieve a constant reverse iterator following to the last element of the reversed list.

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

int main() {
   //create a list
   list<char> char_list = {'a', 'b', 'c', 'd', 'e', 'f'};
   cout<<"List elements are: ";
   for(char v : char_list) {
      cout<<v<<" ";
   }
   auto it = char_list.crend();
   cout<<"\nA constant reverse iterator of the last element: ";
   cout<<*it;
}

Output

This will generate the following output −

List elements are: a b c d e f 
A constant reverse iterator of the last element: 

Example 3

If the list type is a string.

In this example, we are creating a list(type string) named str_list with the values {"Java", "HTML", "CSS", "Angular"}. Then, using the crend() function, we are trying to retrieve a constant reverse iterator following to the last element of the reversed list.

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

int main() {
   //create a list
   list<string> str_list  =  {"Java", "HTML", "CSS", "Angular"};
   cout<<"List elements are: ";
   for(string s : str_list ) {
      cout<<s<<" ";
   }
   auto it = str_list.crend();
   cout<<"\nA constant reverse iterator of the last element: ";
   cout<<*it;
}

Output

The above program produces the following output −

List elements are: Java HTML CSS Angular 
A constant reverse iterator of the last element:
Segmentation fault

Example 4

Using for loop along with the crend() function to retrieve a constant reverse iterator of the reversed list.

In this example, we use the crend() function along with the for loop to loop through the current container {1, 2, 3, 4 ,5, 6, 7, 8, 9, 10} and retrieve a constant reverse iterator of all elements of the reversed list.

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

int main() {
   //create a list
   list<int> num_list = {1, 2, 3, 4 ,5, 6, 7, 8, 9, 10} ;
   cout<<"List elements are: ";
   for(int n : num_list) {
      cout<<n<<" ";
   }
   cout<<"\nThe constant reverse iterator of an elements are : ";
   for (auto it = num_list.crbegin(); it != num_list.crend(); it++) {
      cout<<*it<<" ";
   }
}

Output

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

List elements are: 1 2 3 4 5 6 7 8 9 10 
The constant reverse iterator of an elements are : 10 9 8 7 6 5 4 3 2 1 
Advertisements