C++ STL - Vector in Reverse Order
Last Updated :
02 Nov, 2022
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
C++
// C++ STL program to print all elements
// of a vector in reverse order
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
// Printing original order
cout << "Elements of vector in original order : ";
for (int i : vec) {
cout << i << " ";
}
cout << endl;
// Printing all elements in reverse order
cout << "Elements of vector in reverse order : ";
for (int i = vec.size() - 1; i >= 0; i--)
cout << vec[i] << " ";
return 0;
}
OutputElements 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
C++
// C++ STL program to print all elements
// of a vector in reverse order
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
// Printing original order
cout << "Elements of vector in original order : ";
for (int i : vec) {
cout << i << " ";
}
cout << endl;
// Printing all elements in reverse order
cout << "Elements of vector in reverse order : ";
for (auto it = vec.end() - 1; it >= vec.begin(); it--)
cout << *it << " ";
return 0;
}
OutputElements 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
C++
// C++ STL program to print all elements
// of a vector in reverse order
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
// Printing original order
cout << "Elements of vector in original order : ";
for (int i : vec) {
cout << i << " ";
}
cout << endl;
// Printing all elements in reverse order
cout << "Elements of vector in reverse order : ";
for (auto it = vec.rbegin(); it != vec.rend(); it++) {
cout << *it << " ";
}
return 0;
}
OutputElements of vector in original order : 1 2 3 4 5
Elements of vector in reverse order : 5 4 3 2 1
Similar Reads
Vector Operator = in C++ STL In C++, the vector operator = is used to assign the contents of one vector to another. It allows you to copy elements from one vector to another or initialize a vector with another vector's contents.Letâs take a look at a simple code example:C++#include <bits/stdc++.h> using namespace std; int
3 min read
reverse() in C++ STL In C++, the reverse() is a built-in function used to reverse the order of elements in the given range of elements. This range can be any STL container or an array. In this article, we will learn about reverse() function in C++.Letâs take a look at an example:C++#include <bits/stdc++.h> using n
3 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Vector end() in C++ STL In C++, the vector end() is a built-in method used to obtain an iterator pointing to the theoretical element after the last element of the vector. Even though this iterator does not point to a valid element, it serves as a marker for the end of the vector.Letâs take a look at an example that illustr
3 min read
List of Vectors in C++ STL In C++, the list of vector refers to the list container in which each element is a vector. In this article, we will learn about the list of vectors in C++.Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { list<vector<int>> l = {{1, 3}, {2
3 min read