Open In App

reverse() in C++ STL

Last Updated : 06 Dec, 2024
Comments
Improve
Suggest changes
49 Likes
Like
Report

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:


Output
5 4 3 2 1 

This article covers the syntax, usage, and common examples of reverse() function in C++:

Syntax of reverse()

The reverse() function is defined in the <algorithm> header file.

reverse(first, last);

Parameters:

  • first: Iterator to the first element in the range.
  • last: Iterator to the theoretical element just after the last element in the range.

Return Value:

  • This function does not return any value. It reverses the range in-place.

Examples of reverse()

The below examples show how to use the reverse() function to reverse variety of data containers.

Reverse an Array


Output
5 4 3 2 1 

Reverse a String


Output
dcba

Left Rotate a Vector using reverse()

The left rotation of a vector can be done by using reverse() three times on it.


Output
6 2 9 1 3 

reverse() in C++ STL
Visit Course explore course icon
Article Tags :
Practice Tags :

Similar Reads