VOOZH about

URL: https://www.geeksforgeeks.org/cpp/stdis_permutation-c-stl/

⇱ std::is_permutation in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


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)


Output
Anagrams

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

Comment
Article Tags: