Introduction
We usually use arrays to store elements with the same data types in C++. But arrays have fixed sizes, and we need to know the size of the array beforehand. On the other hand, vectors are dynamic arrays that can change their size accordingly. We can perform various operations on vectors in C++ according to our needs.

In this article, we will look at the different ways by which we can reverse a vector.
Using std::reverse()
We can easily reverse a vector by using the reverse() function provided in STL. We must include the <algorithm> header file to use the reverse function.
Implementation:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
// initializing the vector
vector<char> vect = { 'c', 'o', 'd', 'i', 'n', 'g', ' ', 'n', 'i', 'n', 'j', 'a', 's' };
// Printing the vector
cout<<"Reversing a vector by using the reverse() function"<<endl;
cout << "Vector: ";
for (int i = 0; i < vect.size(); i++)
cout << vect[i];
cout << endl;
// Reversing the vector by using reverse()
reverse(vect.begin(), vect.end());
// Printing the reversed vector
cout << "Reversed Vector: ";
for (int i = 0; i < vect.size(); i++)
cout << vect[i];
cout << endl;
return 0;
}
Output:
Time complexity: O(n)
std::reverse() swaps the elements of the vector. The number of swaps is given by (last-first)/2, therefore the time complexity is O(last-first)/2, or O(n) for the whole vector.
Space complexity: O(1)
As no extra space is used, the time complexity is O(n).