C++ Deque crbegin
C++ Deque crbegin function returns a constant reverse iterator referring to the last element of the deque. An iterator can be incremented or decremented but it cannot modify the content of deque.
Where , crbegin stands for constant reverse beginning.
Syntax
const_reverse_iterator crbegin();
Parameter
It does not contain any parameter.
Return value
It returns a constant reverse iterator pointing to the last element in the deque container.
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 example, crbegin function is used to return an iterator of the last element and for loop is iterated till it reaches the first element of the deque.
Example 2
Let's see a simple example when 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 example, constant reverse iterator is incremented by one. Therefore, it access the second element from backwards.