C++ std::prev_permutation() Function

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

Definition

In C++, std::prev_permutation() changes a range into the previous smaller arrangement in lexicographical order. It returns true if there was one.

When the range is already the first arrangement, it returns false and resets the range to the last one (sorted in descending order).

Syntax

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

C++
std::prev_permutation(v.begin(), v.end());
do { ... } while (std::prev_permutation(v.begin(), v.end()));
CallWhat it does
std::prev_permutation(v.begin(), v.end());Move to the previous arrangement.
do { ... } while (std::prev_permutation(v.begin(), v.end()));Visit every arrangement of a descending range.
Library declarations
C++
template<class BidirIt>
bool prev_permutation(BidirIt first, BidirIt last);

template<class BidirIt, class Compare>
bool prev_permutation(BidirIt first, BidirIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The range to rearrange.
compOptional. A comparison function.

Return Value

Return ValueDescription
trueThe range now holds the previous permutation.
falseThe range was the first permutation, so it was reset to the last one.

C++ Examples

Example 1 — All permutations backward

Print every arrangement of 3 2 1 in reverse dictionary order.

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

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

    do {
        for (int x : v) std::cout << x << ' ';
        std::cout << '\n';
    } while (std::prev_permutation(v.begin(), v.end()));
    return 0;
}

How It Works

  1. The vector starts sorted from large to small, which is the last permutation.
  2. Each call steps to the arrangement just before the current one.
  3. After 1 2 3 there is no earlier one, so the loop stops.

Example 2 — The first permutation

See what happens at the start.

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

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

    bool hasPrev = std::prev_permutation(v.begin(), v.end());

    std::cout << std::boolalpha << "Has previous: " << hasPrev << '\n';
    std::cout << "Now: ";
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. 1 2 3 is the smallest arrangement, so nothing comes before it.
  2. The function returns false.
  3. It wraps around and sets the vector to 3 2 1.

Example 3 — Previous word

Step back one arrangement of letters.

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

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

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

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

How It Works

  1. The six arrangements in order are abc, acb, bac, bca, cab and cba.
  2. cab sits just after bca.
  3. So stepping back gives bca.

Frequently Asked Questions

In C++, std::prev_permutation() rearranges a range into the previous permutation in dictionary order. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns true if a previous permutation exists, and false if the range was the first one and was reset to descending order.
next_permutation() moves forward in dictionary order. prev_permutation() moves backward.
The function returns false and resets the range to the last arrangement.

Next: push_heap()

Adds the last element of a range into an existing heap.

push_heap() 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.

23 people found this page helpful