C++ std::prev_permutation() Function

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.
std::prev_permutation(v.begin(), v.end());
do { ... } while (std::prev_permutation(v.begin(), v.end()));
| Call | What 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
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
| Parameter | Description |
|---|
| first, last | Required. The range to rearrange. |
| comp | Optional. A comparison function. |
Return Value
| Return Value | Description |
|---|
true | The range now holds the previous permutation. |
false | The 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.
#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;
}
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
How It Works
- The vector starts sorted from large to small, which is the last permutation.
- Each call steps to the arrangement just before the current one.
- After 1 2 3 there is no earlier one, so the loop stops.
Example 2 — The first permutation
See what happens at the start.
#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;
}
Has previous: false
Now: 3 2 1
How It Works
- 1 2 3 is the smallest arrangement, so nothing comes before it.
- The function returns
false. - It wraps around and sets the vector to 3 2 1.
Example 3 — Previous word
Step back one arrangement of letters.
#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
- The six arrangements in order are abc, acb, bac, bca, cab and cba.
cab sits just after bca.- 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.
About the author
Developer, cloud engineer, and technical writer
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