C++ Vector rend() Function

Last Updated : 7 Apr 2026

In the C++ programming language, the rend() function is a member function of the STL vector container. It is used to return a reverse iterator that points to the element before the first element of the vector. It is commonly utilized together with the rbegin() function to traverse the vector in reverse order.

The rend() function stands for ‘reverse end’, and is used to point to the element preceding the first element of the vector.

Syntax of the vector rend() function

It has the following syntax:

Return value

It returns a reverse iterator pointing to the reverse end of the vector container.

How does the rend() function work in C++?

When we want to iterate using a vector in reverse order, we have to use these functions:

1. rbegin() Function

The rbegin() function is commonly used to represent the last element of the vector.

2. rend() Function

The rend() function is used to represent the position before the first element of the vector.

These two iterators define the range for reverse traversal of vectors in C++.

Examples of Vector rend() function in C++

Here, we are going to take different examples to demonstrate the working of the vector rend() function in C++.

Example 1: Traversing Vector Elements in Reverse Order

Let us take an example to demonstrate how to traverse vector elements in reverse order using the vector rend() function.

Compile and Run

Output:

5 4 3 2 1

Explanation:

In the given example, we take vector v that is initialized with the elements 1, 2, 3, 4, and 5. After that, we use the reverse iterator with rbegin() and rend() functions to traverse the vector in reverse order. Finally, we use the for loop to print the elements starting from the last element to the first element.

Example 2: Using rend() with Reverse Iterators

Let us take an example to demonstrate the vector rend() function with reverse iterators in C++.

Compile and Run

Output:

Strings are : Computer science, electronics, mechanical
Reverse strings are : mechanical, electrical, electronics, Computer science

Explanation:

In the given example, we have taken a vector of strings that is initialized with different engineering fields. First, we have utilized a normal iterator with begin() and end() functions to print the elements in their original order. After that, we use a reverse iterator with rbegin() and rend() functions to traverse and display the elements in reverse order.

Example 3: Using rend() with while loop

Let us take an example to demonstrate the vector rend() function using the while loop in C++.

Compile and Run

Output:

35 25 20 10

Explanation:

In the given example, we use a while loop instead of a for loop to traverse the vector in reverse order using rbegin() and rend() functions.

Example 4: Reverse Printing with User Input

Let us take an example to demonstrate how to reverse a vector with user input in C++ using the rend() function.

Compile and Run

Output:

Enter 5 numbers:
15
20
45
50
65
The reverse order of the Vector is:
65 50 45 20 15

Explanation:

In the given example, the program asks the user to input five numbers, which are stored in a vector using the push_back() function. When we store 5 random values, the program displays all the elements of the vector in reverse order. It is performed using reverse iterators with the rbegin() and rend() function, which enables traversal from the last element to the first element.


Next TopicC++ Vector