std::is_permutation

From cppreference.com
< cpp‎ | algorithm
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Concepts and utilities: std::Sortable, std::projected, ...
Constrained algorithms: std::ranges::copy, std::ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)
Permutations
is_permutation
(C++11)
Numeric operations
C library
 
Defined in header <algorithm>
(1)
template< class ForwardIt1, class ForwardIt2 >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                     ForwardIt2 first2 );
(since C++11)
(until C++20)
template< class ForwardIt1, class ForwardIt2 >

constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                               ForwardIt2 first2 );
(since C++20)
(2)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                     ForwardIt2 first2, BinaryPredicate p );
(since C++11)
(until C++20)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >

constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                               ForwardIt2 first2, BinaryPredicate p );
(since C++20)
(3)
template< class ForwardIt1, class ForwardIt2 >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                     ForwardIt2 first2, ForwardIt2 last2 );
(since C++14)
(until C++20)
template< class ForwardIt1, class ForwardIt2 >

constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                               ForwardIt2 first2, ForwardIt2 last2 );
(since C++20)
(4)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2, ForwardIt2 last2,

                     BinaryPredicate p );
(since C++14)
(until C++20)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >

constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                               ForwardIt2 first2, ForwardIt2 last2,

                               BinaryPredicate p );
(since C++20)

Returns true if there exists a permutation of the elements in the range [first1, last1) that makes that range equal to the range [first2,last2), where last2 denotes first2 + (last1 - first1) if it was not given.

1,3) Elements are compared using operator==. The behavior is undefined if it is not an equivalence relation.
2,4) Elements are compared using the given binary predicate p. The behavior is undefined if it is not an equivalence relation.

Parameters

first1, last1 - the range of elements to compare
first2, last2 - the second range to compare
p - binary predicate which returns ​true if the elements should be treated as equal.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a, const Type &b);

Type should be the value type of both ForwardIt1 and ForwardIt2. The signature does not need to have const &, but the function must not modify the objects passed to it. ​

Type requirements
-
ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.
-
ForwardIt1, ForwardIt2 must have the same value type.

Return value

true if the range [first1, last1) is a permutation of the range [first2, last2).

Complexity

At most O(N2) applications of the predicate, or exactly N if the sequences are already equal, where N=std::distance(first1, last1).

However if ForwardIt1 and ForwardIt2 meet the requirements of LegacyRandomAccessIterator and std::distance(first1, last1) != std::distance(first2, last2) no applications of the predicate are made.

Possible implementation

template<class ForwardIt1, class ForwardIt2>
bool is_permutation(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 d_first)
{
   // skip common prefix
   std::tie(first, d_first) = std::mismatch(first, last, d_first);
   // iterate over the rest, counting how many times each element
   // from [first, last) appears in [d_first, d_last)
   if (first != last) {
       ForwardIt2 d_last = d_first;
       std::advance(d_last, std::distance(first, last));
       for (ForwardIt1 i = first; i != last; ++i) {
            if (i != std::find(first, i, *i)) continue; // already counted this *i
 
            auto m = std::count(d_first, d_last, *i);
            if (m==0 || std::count(i, last, *i) != m) {
                return false;
            }
        }
    }
    return true;
}

Example

#include <algorithm>
#include <vector>
#include <iostream>
int main()
{
    std::vector<int> v1{1,2,3,4,5};
    std::vector<int> v2{3,5,4,1,2};
    std::cout << "3,5,4,1,2 is a permutation of 1,2,3,4,5? "
              << std::boolalpha
              << std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n';
 
    std::vector<int> v3{3,5,4,1,1};
    std::cout << "3,5,4,1,1 is a permutation of 1,2,3,4,5? "
              << std::boolalpha
              << std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n';
}

Output:

3,5,4,1,2 is a permutation of 1,2,3,4,5? true
3,5,4,1,1 is a permutation of 1,2,3,4,5? false

See also

generates the next greater lexicographic permutation of a range of elements
(function template)
generates the next smaller lexicographic permutation of a range of elements
(function template)