[alg.is.permutation]

# 26 Algorithms library [[algorithms]](./#algorithms)

## 26.6 Non-modifying sequence operations [[alg.nonmodifying]](alg.nonmodifying#alg.is.permutation)

### 26.6.14 Is permutation [alg.is.permutation]

[ð](#lib:is_permutation)

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

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L5722)

Let last2 be first2 + (last1 - first1) for the overloads with no parameter named last2,
and let pred be equal_to{} for the overloads with no parameter pred[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L5728)

*Mandates*: ForwardIterator1 and ForwardIterator2 have the same value type[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L5732)

*Preconditions*: The comparison function is an equivalence relation[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L5736)

*Returns*: If last1 - first1 != last2 - first2, return false[.](#4.sentence-1)

Otherwise return true if there exists a permutation of the elements
in the range [first2, last2),
beginning with ForwardIterator2 begin,
such that equal(first1, last1, begin, pred) returns true;
otherwise, returns false[.](#4.sentence-2)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L5746)

*Complexity*: No applications of the corresponding predicate
if ForwardIterator1 and ForwardIterator2 meet the requirements of random access iterators andlast1 - first1 != last2 - first2[.](#5.sentence-1)

Otherwise, exactly last1 - first1 applications
of the corresponding predicate
if equal(first1, last1, first2, last2, pred) would return true;
otherwise, at worst O(N2), where N has the value last1 - first1[.](#5.sentence-2)

[ð](#lib:is_permutation_)

`template<[forward_iterator](iterator.concept.forward#concept:forward_iterator "24.3.4.11 Concept forward_­iterator [iterator.concept.forward]") I1, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I1> S1, [forward_iterator](iterator.concept.forward#concept:forward_iterator "24.3.4.11 Concept forward_­iterator [iterator.concept.forward]") I2,
         [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I2> S2, class Proj1 = identity, class Proj2 = identity,
         [indirect_equivalence_relation](indirectcallable.indirectinvocable#concept:indirect_equivalence_relation "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I1, Proj1>,
                                       projected<I2, Proj2>> Pred = ranges::equal_to>
  constexpr bool ranges::is_permutation(I1 first1, S1 last1, I2 first2, S2 last2,
                                        Pred pred = {},
                                        Proj1 proj1 = {}, Proj2 proj2 = {});
template<[forward_range](range.refinements#concept:forward_range "25.4.6 Other range refinements [range.refinements]") R1, [forward_range](range.refinements#concept:forward_range "25.4.6 Other range refinements [range.refinements]") R2,
         class Proj1 = identity, class Proj2 = identity,
         [indirect_equivalence_relation](indirectcallable.indirectinvocable#concept:indirect_equivalence_relation "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R1>, Proj1>,
                                       projected<iterator_t<R2>, Proj2>> Pred = ranges::equal_to>
  constexpr bool ranges::is_permutation(R1&& r1, R2&& r2, Pred pred = {},
                                        Proj1 proj1 = {}, Proj2 proj2 = {});
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L5776)

*Returns*: If last1 - first1 != last2 - first2, return false[.](#6.sentence-1)

Otherwise return true if there exists a permutation of the elements
in the range [first2, last2), bounded by [pfirst, plast),
such thatranges​::​equal(first1, last1, pfirst, plast, pred, proj1, proj2) returns true;
otherwise, returns false[.](#6.sentence-2)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L5786)

*Complexity*: No applications of the corresponding predicate and projections if

- [(7.1)](#7.1)

for the first overload,
  * [(7.1.1)](#7.1.1)

S1 and I1 model [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<S1, I1>,

  * [(7.1.2)](#7.1.2)

S2 and I2 model [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<S2, I2>, and

  * [(7.1.3)](#7.1.3)

last1 - first1 != last2 - first2;

- [(7.2)](#7.2)

for the second overload,R1 and R2 each model [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]"), andranges​::​distance(r1) != ranges​::​distance(r2)[.](#7.sentence-1)

Otherwise, exactly last1 - first1 applications
of the corresponding predicate and projections
if ranges​::​equal(​first1, last1, first2, last2, pred, proj1, proj2) would return true;
otherwise, at worst O(N2), where N has the value last1 - first1[.](#7.sentence-2)
