C++ std::reverse() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::reverse() flips a range so the first element becomes the last and the last becomes the first. The change happens in place.

It works on vectors, strings, arrays and any container that supports bidirectional iterators.

Syntax

Add #include <algorithm>, then call std::reverse(). Start with these two lines.

C++
std::reverse(v.begin(), v.end());
std::reverse(v.begin() + 1, v.begin() + 5);
CallWhat it does
std::reverse(v.begin(), v.end());Reverse the whole container.
std::reverse(v.begin() + 1, v.begin() + 5);Reverse only part of it.
Library declarations
C++
template<class BidirIt>
void reverse(BidirIt first, BidirIt last);

Parameters

ParameterDescription
first, lastRequired. The range to reverse.

Return Value

Return ValueDescription
voidNothing is returned. The range is changed in place.

C++ Examples

Example 1 — Reverse a vector

Flip five numbers.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};

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

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The call swaps the first and last elements, then the second and second-to-last.
  2. The middle element stays where it is.
  3. The vector now reads from 5 down to 1.

Example 2 — Reverse a string

Flip the letters of a word.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string word = "CodeToFun";

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

    std::cout << word << '\n';
    return 0;
}

How It Works

  1. A string is a range of characters, so it can be reversed like a vector.
  2. The first letter C swaps with the last letter n, and so on.
  3. Capital letters move with their characters.

Example 3 — Reverse part of a vector

Flip only the middle of a vector.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6};

    std::reverse(v.begin() + 1, v.begin() + 5);

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The range starts at index 1 and stops before index 5.
  2. Only 2, 3, 4 and 5 are flipped.
  3. The first element 1 and the last element 6 stay in place.

Frequently Asked Questions

In C++, std::reverse() reverses the order of the elements in a range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). The elements in the range are reversed in place.
Call std::reverse(s.begin(), s.end()). It changes the string in place.
Use std::reverse_copy() and write the result to another container.

Next: reverse_copy()

Copies a range into a destination in reverse order.

reverse_copy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful