How to reverse a Vector using STL in C++?

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to understand how to reverse a vector using STL in C++.

For reversing a given vector we will be using the reverse() function from the STL library in C++.

Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
//collecting the vector
vector<int> a = { 1, 45, 54, 71, 76, 12 };
cout << "Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
//reversing the vector
reverse(a.begin(), a.end());
cout << "Reversed Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
return 0;
}

Output

Vector: 1 45 54 71 76 12
Reversed Vector: 12 76 71 54 45 1
Updated on 02-Mar-2020 10:49:46