C++ Deque end() Function

Last Updated : 14 May 2026

Deque end() Function

In the C++ programming language, the deque (double-ended queue) is a container class that is a part of the Standard Template Library (STL). It allows fast insertion and deletion of elements at both the beginning and the end of the deque.

In C++, the Deque end() function is commonly utilized to return an iterator that points to the "past-the-last" element of the deque. If the deque container is empty, the end() function returns the same as the begin() function. The "past-the-last" is the element that is used to follow the last element, and it marks the end boundary for forward iteration.

Syntax

It has the following syntax:

Parameters

  • It does not contain any parameters.

Return Value

It returns an iterator referring to the past-the-last element. If the deque is constant, it also returns the const_iterator.

Examples of Deque end() Function

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

Example 1: Traverse a Deque Using begin() and end()

This example demonstrates how to traverse all deque elements using the begin() and end() functions.

Output:

10 20 30 40 50

Explanation:

In this example, we have taken a deque of integers and traversed this deque using an iterator. After that, we use the begin() function that gives the iterator to the first element, while the end() function marks the position just after the last element. Finally, the loop executes until itr reaches the end() function, which prints all elements of the deque in order.

Example 2: Access the Last Element Using end()

This example demonstrates how to access the last element of a deque using the end() function.

Output:

Last element of the deque is: DSA

Explanation:

In this example, we have created a deque of strings and use the end() function to get an iterator pointing past the last element. After that, by decrementing the iterator with --it, we move it to the actual last element, which is then displayed as "DSA".

Example 3: Use the STL find() Algorithm with end()

This example demonstrates how to use the STL find() algorithm with the end() function to search elements in a deque.

Output:

Element is found in the Deque: 25

Explanation:

In this example, we have created a deque of integers and also used the find() algorithm to find the value 25 with the begin() and end() functions. If the iterator returned is not equal to the dq.end() function, it specifies that the element is available in the deque, and the found value is displayed on the screen.


Next TopicC++ Deque