C++ Deque crbegin
The crbegin function in C++ for deques provides a constant reverse iterator that points to the final element in the deque. This iterator allows for traversal in reverse order but does not permit modification of the deque's contents.
Whereas, crbegin represents the constant reverse beginning iterator.
Syntax
const_reverse_iterator crbegin();
Parameter
It does not contain any parameter.
Return value
It yields a constant reverse iterator that references the final element within the deque data structure.
Example 1
Let's see a simple example
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<int> i={10,20,30,40,50};
deque<int>::const_reverse_iterator citr;
for(citr=i.crbegin();citr!=i.crend();++citr)
{
cout<<*citr;
cout<<" ";
}
return 0;}
Output:
50 40 30 20 10
In this instance, the crbegin method is employed to retrieve an iterator pointing to the final element, and the for loop is executed until it reaches the initial element of the deque.
Example 2
Let's explore a basic scenario where an iterator is incremented.
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<string> fruit={"electronics","computer science","mechanical","electrical"};
deque<string>::const_reverse_iterator citr=fruit.crbegin()+1;
cout<<*citr;
return 0;
}
Output:
mechanical
In this instance, the constant reverse iterator is advanced by one, allowing it to retrieve the second element from the end.