×

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

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

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

C++ Enumeration

C++ Enumeration In C++, Enum is a special data type that contains some fixed sets of components that have various applications in the programming. Enum works fine with fixed constant sets...

3 minutes read.

goto statement in C and C++

goto statement in C and C++ The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used...

3 minutes read.

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

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

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

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

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

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

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.