×

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need to import the algorithm statement.

  • Example of reverse function using vector standard template library.
#include <vector>
#include <iostream>
// The following header needs to be included to use std::reverse()
#include <algorithm>


using namespace std;


int main() {
  std::vector<int> values = {
    3,4,6,2,4,7,3,6,3,6,3,6,98
  };


  std::cout << "Before reversing: ";
  for(int x: values) {
    std::cout << x << ' ';
  }


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


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

Output:

Reverse function in C++

Explanation:

In the above code, we have declared a vector data type in which we have given some data. To use the reverse function, we have imported the algorithm library. We reversed the vector data using the reverse function and printed the output.

  • Example of Reverse function for the array
#include <iostream>
// The following header needs to be included to use std::reverse()
#include <algorithm>
// The following header needs to be included to use std::begin() and std::end()
#include <iterator>


using namespace std;


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


  std::cout << "Before reversing: ";
  for(int x: arr) {
    std::cout << x << ' ';
  }


  // built-in array has no begin() and end() member functions
  // but we can pass it to std::begin() and std::end()
  // in order to get the iterators we need


  std::reverse(std::begin(arr), std::end(arr));


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

Output:

Reverse function in C++

Explanation

In this example, we have used the reverse function to reverse the array. To use the reverse function, we have imported the algorithm library. For the parameters, we have used functions present in the iterator library to send the starting and ending values of the array and then printed the reverse of the array.

  • Example of reverse function for a string
#include <string>
#include <iostream>
// The following header needs to be included to use std::reverse()
#include <algorithm>


using namespace std;


int main() {
  std::string sentence = "Hello, World!";


  std::cout << "Before reversing: " << sentence << '\n';
  std::reverse(sentence.begin(), sentence.end());
  std::cout << "After reversing: " << sentence << '\n';
}

Output:

Reverse function in C++

Explanation:

In the above example, we have used the reverse function to reverse a string. To use the reverse function, we have imported the algorithm library. Using the reverse statement, we have reversed the string by passing starting and ending character of the string .


Related Topics

Destructor

Let’s discover the C++ destructor, virtual and pure virtual destructors, and when destructors are invoked and their rules in this lesson. Destructors : A member function that destroys an object is known as...

5 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

4 minutes read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

Compile Time Polymorphism in C++

What is Polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in...

4 minutes read.

Nullptr in C++

What is Nullptr in C++? A null pointer value is represented by the term nullptr. Use a null pointer value to indicate that a native pointer type, inner pointer, or object...

3 minutes read.

Bit Manipulation in C++

The high-level language in which we communicate is not understood by the computer. As a result, there existed a standard mechanism for understanding any instruction sent to the computer. At...

5 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

C++ Heap Sort

Heapsort is executed on the structure of the heap data. We know heap is a complete tree in binary form. The heap tree can be of two different types: Min-heap,...

3 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.

Single level Inheritance

Inheritance is a fundamental element of C++’s Object-Oriented Programming (OOP). It allows a class (called the derived class) to inherit characteristics and attributes from another class (called the base class)....

5 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

Returning Multiple Values from a Function using Tuple and Pair in C++

We may come across many situations where after the driver code's execution is performed in a code block, the return should be either multiple values or a single value possibly...

4 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.