Find All Permutations of an Array using STL in C++ Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice The permutation of an array refers to a rearrangement of its elements in every possible order. In this article, we will learn how to generate all possible permutation of an array using STL in C++.The simplest method to find all the permutations of an array is to use next_permutation(). The array has to be sorted in ascending order because this function generate the next lexicographically larger permutation of an array. The below example shows how to implement this method: C++ #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); sort(arr, arr + n); // Generate all permuatation of an array do { for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } while (next_permutation(arr, arr + n)); return 0; } Output1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Explanation: The next_permutation() method returns true if the next permutation exists and rearranges the arr to it and returns false if it doesn't exits. So, we used a loop in the above program that runs till next_permutation() returns false.Note: This function modifies the given array to generate its next permutation.Using prev_permutation() FunctionThe prev_permutation() does just the opposite of the prev_permutation() function and generate all the previous permutation of the given array. But the given array should be sorted in the descending order. C++ #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); sort(arr, arr + n, greater<int>()); // Generate all permuatation of an array do { for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } while (prev_permutation(arr, arr + n)); return 0; } Output3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3 Comment More infoAdvertise with us C code_r Follow Improve Article Tags : C++ Programs C++ STL cpp-array Permutation and Combination CPP Examples +2 More Explore Introduction to C++Introduction to C++ Programming Language2 min readHeader Files in C++5 min readSetting up C++ Development Environment8 min readDifference between C and C++3 min readBasicsC++ Data Types7 min readC++ Variables4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readC++ Loops7 min readFunctions in C++8 min readC++ Arrays8 min readStrings in C++5 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readC++ OOPInheritance in C++10 min readC++ Polymorphism5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Containers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library2 min readPractice C++C++ Interview Questions and Answers1 min readTop C++ DSA Related ProblemsC++ Programming Examples7 min read Like