×

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

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

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.

Input Iterators in C++

What are input iterators? Input iterators are used in sequence for carrying out input operations where each value is read-only. It is pointed by the iterator and further incremented. All the iterators...

4 minutes read.

C++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

How to Reverse a String in C++ using For Loop

For Loop: We may loop through a certain section of C++ code repeatedly using the for loop. A for loop is carried out if the test expression yields a true result. The...

4 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

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

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.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

C++ Virtual Destructor

In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The...

4 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.

Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty...

3 minutes read.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.