- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
List crbegin() and crend() function in C++ STL
Given is the task to show the working of list crbegin() and crend() functions in C++.
The list::crbegin() and list::crend() functions are a part of the C++ standard template library.
<list> header file should be included to call these functions.
- list::crbegin()
This function returns the constant iterator which points to the end element of the list which will be the reverse beginning of the list. It can be used for Backtracking the list but it cannot change the values in the list which means crbegin() function can be used for iteration only.
Syntax
List_Name.crbegin()
Parameters
The function does not accept any parameter.
Return Value
The function returns a constant reverse iterator that points at the reverse beginning element of the list, that is, the end of the list.
- list::crend()
This function returns the constant iterator which points to the end element of the list. It can be used for Backtracking the list but it cannot change the values in the list which means crend() function can be used for iteration only.
Syntax
List_Name.crend()
Parameters
The function does not accept any parameter.
Return Value
The function returns a constant reverse iterator that points at the reverse end of the list, that is the beginning of the list.
Example
Input: list<int> Lt={99,34,55} Output: The last element is 55
Explanation −
Here we created a list with elements 99, 34 and 55. Then we called the crbegin() function that points at the reverse beginning of the list, that is the end of the list.
So when we print it, the output generated is 55, which is the last element of the list.
Approach used in the below program as follows −
- First create a list, let us say “Ld” of type int and assign it some values.
- Then start a loop for traversing the list.
- Then create an object “itr” of type auto inside the loop for storing the return values of crend() and crbegin() function. Initialize “itr” by giving it the first element of the list using the crend() function.
- Then specify the terminating condition of the loop by writing “itr” not equal to the last element of the list using the crbegin() function.
- Print the value of *itr.
Algorithm
Start Step 1->In function main() Initialize list<int> Lt={} Loop For auto itr = Lt.crend() and itr != Lt.crbegin() and itr++ Print *itr End Stop
Example
#include<iostream> #include<list> using namespace std; int main() { list<int> Lt = { 33,44,55,66 }; //Printing the elements of the list cout <<"The elements of the list are : " <<"\n"; for (auto itr = Lt.crend(); itr != Lt.crbegin(); itr++) cout << *itr << " "; return 0; }
Output
If we run the above code it will generate the following output −
The elements of the list are : 4