Table of contents
1.
Introduction
2.
Using std::reverse()
2.1.
Implementation:
3.
Using Reverse Iterators
3.1.
Implementation:
4.
By swapping the elements
4.1.
Implementation:
5.
Frequently Asked Questions
5.1.
What are the different operations that can be performed on vectors?
5.2.
What are iterators?
5.3.
What header file should be included to use vectors in our code?
5.4.
How to reverse a 2D vector in C++?
6.
Conclusion
Last Updated: Jan 10, 2025
Easy

Different Ways to Reverse a Vector

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Different Ways to Reverse a Vector

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;
}
You can also try this code with Online C++ Compiler
Run Code

 

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).

Using Reverse Iterators

Implementation:

#include <algorithm>
#include <iostream>
#include <vector>
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 reverse iterators"<<endl;
    cout << "Vector: ";
    for (int i = 0; i < vect.size(); i++)
        cout << vect[i];
    cout << endl;

    //reversing the vector
    vector<char> v2 (vect.rbegin(),vect.rend());
    vect.swap(v2);

    // Printing the reversed vector
    cout << "Reversed Vector: ";
    for (int i = 0; i < vect.size(); i++)
        cout << vect[i];
    cout << endl;
    
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Time Complexity: O(1)

As we are swapping the addresses of two vectors, the time complexity is constant O(1).

Space complexity: O(n)

As we have to create a new vector, the space complexity is O(n).

By swapping the elements

We can simply use the swap() function to swap the elements of the vectors.

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 swapping the elements"<<endl;
    cout << "Vector: ";
    for (int i = 0; i < vect.size(); i++)
        cout << vect[i];
    cout << endl;
 
    // swapping 
    for (auto first = vect.begin(),last=vect.end()-1; first<last; first++,last--) {
        swap(*first, *last);
    }
 
    // Printing the reversed vector
    cout << "Reversed Vector: ";
    for (int i = 0; i < vect.size(); i++)
        cout << vect[i];
    cout << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Time complexity: O(n)

Like std::reverse(), swap() also swaps the elements of the vector. The time complexity of the swaps is given by O(last-first)/2, or O(n).

Space complexity: O(1)

Space complexity is O(1) as no new space is created.
 

Must Read  C Program to Reverse a Number

Frequently Asked Questions

What are the different operations that can be performed on vectors?

We can perform various operations like updating the elements, deleting the elements, sorting, reversing, etc.

What are iterators?

Iterators are used to travel from one element to another element. They can be used for any type of container.

What header file should be included to use vectors in our code?

Vector.h header file must be used.

How to reverse a 2D vector in C++?

To reverse a 2D vector in C++, reverse each row individually, then reverse the order of the rows in the vector. This ensures both elements within rows and the row order are reversed.

Conclusion

Vectors are dynamic arrays, and different operations can be performed on them. In this article, we talked about how to reverse a vector. We looked at three primary ways to reverse a vector and the time and space complexity involved in them. 

Live masterclass