std::is_permutation in C++ STL

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 <iostream>
#include <algorithm>
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

  • first1, last1 - Range of elements in the first sequence
  • first2, last2 - Range of elements in the second sequence
  • p - Binary predicate returning true if two elements are considered equal

Return Value

  • true - if all elements in the first range are present in the second range in any order.
  • false - if any element is missing or extra.

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 <iostream>
#include <algorithm>
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;
}

Output
Anagrams

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

Comment