C++ Deque crend() Function

Last Updated : 14 May 2026

Deque crend() Function

In C++, the deque::crend() function returns a constant reverse iterator that points to the position before the first element of the deque container.

It is mainly used together with the crbegin() function to traverse deque elements in reverse order in a read-only manner. Since the iterator returned by crend() is constant, it cannot modify the elements of the deque. The crend() function is useful for safe reverse traversal of constant deque containers using STL iterators.

C++ Deque crend() Function

Syntax

It has the following syntax:

Or

Parameters

It does not contain any parameter.

Return Value

It returns a constant reverse iterator referring to the element preceding the first element of the deque container.

Examples of Deque crend() Function

Here, we are going to discuss several examples to demonstrate the deque crend() function.

Example 1: Traverse a Character Deque in Reverse Using crend()

This example demonstrates how to traverse a deque of characters in reverse order using the crbegin() and crend() functions.

Output:

Reverse deque: potpal

Explanation:

In this example, we have created a deque of 'char' type that contains some elements inside it. After that, we have declared a constant reverse iterator that we have executed from the beginning of the deque using the 'crbegin()' method. Next, we use the while loop, which iterates till the iterator 'citr' does not become equal to the 'c.crend()' function. In the loop, we have displayed the iterator and pre-incremented the iterator by 1. In the output, we have got the character elements displayed from the end to the beginning, i.e., 'laptop' converted to 'potpal'.

Example 2: Reverse Traverse an Integer Deque Using crend()

This example demonstrates how to traverse integer elements in reverse order using a constant reverse iterator with the crend() function.

Output:

After performing the reverse deque, we get : 
12
90
56
34
42 

Explanation:

In this example, we have created a deque of integer type that contains several integer numbers. After that, we have declared a constant reverse iterator that we have executed from the beginning of the deque using the 'crbegin()' method. Next, we use the while loop that iterates till the iterator 'citr' does not become equal to the 'val.crend()' function. In the loop, we have displayed the iterator and pre-incremented the iterator by 1. In the output, we have the integer elements displayed from the end to the beginning.

Example 3: Use crend() with a String Deque

This example demonstrates how to use the crend() function with a deque of strings for read-only reverse traversal.

Output:

Names in the reverse order (read-only traversal):
Robert
Johnson
Peter
Alexa

Explanation:

In this example, we create a deque of strings named name that contains four elements. After that, we use a loop that uses the crbegin() and crend() functions to traverse the deque in reverse order, which displays elements from the last to the first (in reverse order). As both functions are constant reverse iterators, the elements can only be accessed (read) and not changed during traversal.


Next TopicC++ Deque