Open In App

C++ STL - Vector in Reverse Order

Last Updated : 02 Nov, 2022
Comments
Improve
Suggest changes
1 Like
Like
Report

Prerequisite: Vectors in C++

A vector can be printed in reverse order with the following methods:

  • By traversing in the backward direction using indexing
  • By traversing in the backward direction using begin() and end() functions in C++ STL
  • By traversing in the backward direction using rbegin() and rend() functions in C++ STL

Examples:

Input: vector = {1, 2, 3, 4, 5}
Output: {5, 4, 3, 2, 1}

Input: vector = {10,5,20,15,25}
Output: {25, 15, 20, 5, 10}

1. Traversing the vector in a backward direction using indexing


Output
Elements of vector in original order : 1 2 3 4 5 
Elements of vector in reverse order : 5 4 3 2 1 

2. Using begin() and end() functions 


Output
Elements of vector in original order : 1 2 3 4 5 
Elements of vector in reverse order : 5 4 3 2 1 

3. Using rbegin() and rend() function


Output
Elements of vector in original order : 1 2 3 4 5 
Elements of vector in reverse order : 5 4 3 2 1 

Practice Tags :

Similar Reads