C++ std::next_permutation() Function

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

Definition

In C++, std::next_permutation() changes a range into the next bigger arrangement in lexicographical (dictionary) order. It returns true if there was a next one.

When the range is already the last arrangement, it returns false and resets the range to the first (sorted) one. A loop with it prints every permutation.

Syntax

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

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

template<class BidirIt, class Compare>
bool next_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 next permutation.
falseThe range was the last permutation, so it was reset to the first (sorted) one.

C++ Examples

Example 1 — All permutations

Print every arrangement of 1 2 3.

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

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

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

How It Works

  1. The vector starts sorted, so the loop begins at the first permutation.
  2. The do body prints the current arrangement, then next_permutation() moves on.
  3. After 3 2 1 the function returns false and the loop ends.

Example 2 — The last permutation

See what happens at the end.

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

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

    bool hasNext = std::next_permutation(v.begin(), v.end());

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

How It Works

  1. 3 2 1 is the biggest arrangement, so there is no next one.
  2. The function returns false.
  3. It also resets the vector to the first arrangement, 1 2 3.

Example 3 — Next bigger number

Use the digits of a number as text.

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

int main() {
    std::string digits = "1243";

    std::next_permutation(digits.begin(), digits.end());

    std::cout << "Next number: " << digits << '\n';
    return 0;
}

How It Works

  1. The digits 1243 are treated as a string and compared like a dictionary.
  2. The tail 43 is already the biggest, so the function must change the 2 as well.
  3. The next arrangement is 1324.

Frequently Asked Questions

In C++, std::next_permutation() rearranges a range into the next 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 next permutation exists, and false if the range was the last one and was reset to sorted order.
Sort the range, then use a do-while loop with std::next_permutation().
false. It also resets the range to the first, sorted, arrangement.

Next: none_of()

Checks that no element in a range matches a condition.

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

22 people found this page helpful