C++ <deque> - crend() Function
The C++ deque::crend function returns the constant reverse iterator (const_reverse_iterator) pointing to the element preceding the first element (reversed past-the-last element) of the deque.
Note: A const_reverse_iterator is an iterator that points to constant value and iterates in backward direction. Increasing a const_reverse_iterator results into moving to the beginning of the deque container and decreasing it results into moving to the end of the deque container. Along with this, it cannot be used to modify the contents it points to, even if the deque element is not itself constant.
Syntax
const_reverse_iterator crend() const noexcept;
Parameters
No parameter is required.
Return Value
A const_reverse_iterator to the reversed past-the-last element of the sequence container.
Time Complexity
Constant i.e, Θ(1).
Example:
In the example below, the deque::crend function returns the const_reverse_iterator pointing to the element preceding the first element of the deque MyDeque.
#include <iostream> #include <deque> using namespace std; int main (){ deque<string> MyDeque{"Alpha","Coding","Skills"}; deque<string>::const_reverse_iterator crit; crit = MyDeque.crend(); crit--; cout<<*crit<<" "; crit--; cout<<*crit<<" "; crit--; cout<<*crit<<" "; return 0; }
The output of the above code will be:
Alpha Coding Skills
Example:
Lets see another example where the deque called MyDeque contains integer values and deque::crend function is used with deque::crbegin function to specify a range including all elements of the deque container.
#include <iostream> #include <deque> using namespace std; int main (){ deque<int> MyDeque{10, 20, 30, 40, 50}; deque<int>::const_reverse_iterator crit; for(crit = MyDeque.crbegin(); crit != MyDeque.crend(); ++crit) cout<<*crit<<" "; return 0; }
The output of the above code will be:
50 40 30 20 10
❮ C++ <deque> Library