What is the std::reverse() function in C++?

In this blog post, you will learn what std::reverse() is with the help of a C++ programming example. So let’s first understand std::reverse().

std::reverse() is a built-in function in C++’s Standard Template Library. It is defined in <algorithm> the header file. It reverses the order of the elements in the given range [first, last).

Now I believe you are thinking, why I have written [first, last) ?

Don’t worry I am explaining why I denote the range by two different braces.

The range used is[first, last), which means the range contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

Syntax of reverse():

template< class BidirIt >
void reverse( BidirIt first, BidirIt last ); (until C++20)



template< class BidirIt >
constexpr void reverse( BidirIt first, BidirIt last ); (since C++20)

 

Parameters:

  • first, last – the range of elements to reverse

Note: BidirIt is an iterator that can be used to access any elements of a container in both forward and backward directions.

 

Example-1:  Using std::reverse with a std::vector.

#include <vector>
#include <iostream>
using namespace std;

// algorithm needs to be included to use std::reverse()
#include <algorithm>

using namespace std;

int main()
{
    vector<int> values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    cout << "Before reversing: \n";
    for(int arr:values)
    {
        cout<< arr<< ' ';
    }

    reverse(values.begin(), values.end());

    cout << "\n\nAfter reversing: \n";
    for(int arr: values)
    {
        cout << arr << ' ';
    }
    
    return 0;
}

Output:

Before reversing:
1 2 3 4 5 6 7 8 9 10

After reversing:
10 9 8 7 6 5 4 3 2 1

 

Example-2: Using std::reverse with a std::string.

#include <vector>
#include <iostream>
using namespace std;

// algorithm needs to be included to use std::reverse()
#include <algorithm>

using namespace std;

int main()
{
    string str = "Aticleworld.com";

    cout << "Before reversing: \n";
    cout<< str<<endl;

    //Use the reverse to reverse string
    reverse(str.begin(), str.end());

    cout << "\n\nAfter reversing: \n";
    cout<< str<<endl;
    return 0;
}

Output:

Before reversing:
Aticleworld.com


After reversing:
moc.dlrowelcitA

 

Example 3: Using std::reverse with a built-in array type.

#include <vector>
#include <iostream>
using namespace std;

// algorithm needs to be included to use std::reverse()
#include <algorithm>

using namespace std;

int main()
{
    int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    cout << "Before reversing: \n";
    for(int arr:values)
    {
        cout<< arr<< ' ';
    }

    /*
      Array don't has begin() and end() member functions.
      so we can pass it to begin() and end() to get the iterators
    */
    reverse(begin(values), end(values));

    cout << "\n\nAfter reversing: \n";
    for(int arr: values)
    {
        cout << arr << ' ';
    }

    return 0;
}

Output:

Before reversing:
1 2 3 4 5 6 7 8 9 10

After reversing:
10 9 8 7 6 5 4 3 2 1

 

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *