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.
It has the following syntax:
It returns a reverse iterator pointing to the reverse end of the vector container.
When we want to iterate using a vector in reverse order, we have to use these functions:
The rbegin() function is commonly used to represent the last element of the vector.
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++.
Here, we are going to take different examples to demonstrate the working of the vector rend() function in C++.
Let us take an example to demonstrate how to traverse vector elements in reverse order using the vector rend() function.
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.
Let us take an example to demonstrate the vector rend() function with reverse iterators in C++.
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.
Let us take an example to demonstrate the vector rend() function using the while loop in C++.
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.
Let us take an example to demonstrate how to reverse a vector with user input in C++ using the rend() function.
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.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India