std::is_permutation in C++ STL (original) (raw)

Last Updated : 20 Jan, 2026

In C++, the STL function std::is_permutation checks whether one sequence is a permutation of another, i.e., whether both sequences contain the same elements in any order. It uses the == operator (or a custom predicate) for comparison. This function was introduced in C++11.

C++ `

#include #include using namespace std;

int main() { int A[] = {1, 7, 0, 2}; int B[] = {0, 7, 2, 1};

if (is_permutation(A, A+4, B))
    cout << "B is a permutation of A";
else
    cout << "B is not a permutation of A";

return 0;

}

`

Output

B is a permutation of A

**Explanation: All elements in A exist in B in any order, so the function returns true.

Syntax

**Basic Version (C++11)

template <class ForwardIterator1, class ForwardIterator2>
bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);

**With Binary Predicate (C++11)

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate p);

**Range-Specified Version (C++14)

template <class ForwardIterator1, class ForwardIterator2>
bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);

**Range + Predicate (C++14)

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate p);

Parameters

Return Value

**Note: Only the number of elements in [first1, last1] is considered. If the second sequence is shorter, the behavior is undefined.

**Example : Strings (Anagram Check)

C++ `

#include #include using namespace std;

int main() { string A = "SILENT"; string B = "LISTEN";

if (is_permutation(A.begin(), A.end(), B.begin()))
    cout << "Anagrams";
else
    cout << "Not Anagrams";

return 0;

}

`

**Explanation: All letters in A appear in B, confirming that the strings are anagrams.