list crbegin() and crend() function in C++ STL
The list::crbegin() is a built-in function in c++ STL that returns a constant reverse iterator which points to the last element of the list i.e reversed beginning of container. The elements cannot be modified or changed as the iterator is a constant one.
Syntax:
list_name.crbegin()
Parameters: The function does not accept any parameters.
Return value: It returns a random access reverse iterator which points to the reverse beginning of the list.
Below program illustrates the above function:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
list< int > lis = { 10, 20, 30, 40, 50 };
cout << "The last element is: " << *lis.crbegin();
cout << "\nList: " ;
for ( auto it = lis.crbegin(); it != lis.crend(); ++it)
cout << *it << " " ;
return 0;
}
|
Output
The last element is: 50
List: 50 40 30 20 10
Time Complexity: O(1)
Auxiliary Space: O(1)
The list::crend() is a built-in function in C++ STL that returns a constant reverse iterator which points to the theoretical element preceding the first element in the list i.e. the reverse end of the list. The elements cannot be modified or changed as the iterator is a constant one.
Syntax:
list_name.crend()
Parameters: The function does not accept any parameters.
Return value: It returns a constant random reverse iterator which points to the reverse end of the list.
Below program illustrates the above function:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
list< int > lis = { 7, 6, 5, 4, 3, 2 };
cout << "List: " << endl;
for ( auto it = lis.crbegin(); it != lis.crend(); ++it)
cout << *it << " " ;
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Let us see the differences in a tabular form -:
|
list crbegin() |
list crend() |
1. |
It is used to return a const_reverse_iterator pointing to the last element in the container
|
It is used to return a const_reverse_iterator pointing to the theoretical element preceding the first element in the container
|
2. |
Its syntax is -:
const_reverse_iterator crbegin(); |
Its syntax is -:
const_reverse_iterator crend();
|
3. |
It does not take any parameters.
|
It does not take any parameters.
|
4. |
Its complexity is constant.
|
Its complexity is constant.
|
5. |
Its iterator validity does not changes.
|
Its iterator validity does not changes.
|
Last Updated :
26 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...