On this page
std::is_permutation
Defined in header <algorithm> |
||
---|---|---|
(1) | ||
|
(since C++11) (until C++20) |
|
|
(since C++20) | |
(2) | ||
|
(since C++11) (until C++20) |
|
|
(since C++20) | |
(3) | ||
|
(since C++14) (until C++20) |
|
|
(since C++20) | |
(4) | ||
|
(since C++14) (until C++20) |
|
|
(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.
operator==
. The behavior is undefined if it is not an equivalence relation.
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:
|
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 \(\scriptsize \mathcal{O}(N^2)\)O(N2) applications of the predicate, or exactly \(\scriptsize N\)N if the sequences are already equal, where \(\scriptsize N\)N is 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.
Note
The std::is_permutation
can be used in testing, namely to check the correctness of rearranging algorithms (e.g. sorting, shuffling, partitioning). If x
is an original range and y
is a permuted range then std::is_permutation(x, y) == true
means that y
consist of "the same" elements, maybe staying at other positions.
Possible implementation
|
Example
#include <algorithm>
#include <iostream>
template<typename Os, typename V>
Os& operator<<(Os& os, V const& v)
{
os << "{ ";
for (auto const& e : v)
os << e << ' ';
return os << '}';
}
int main()
{
static constexpr auto v1 = {1, 2, 3, 4, 5};
static constexpr auto v2 = {3, 5, 4, 1, 2};
static constexpr auto v3 = {3, 5, 4, 1, 1};
std::cout << v2 << " is a permutation of " << v1 << ": " << std::boolalpha
<< std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n'
<< v3 << " is a permutation of " << v1 << ": "
<< 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) |
|
(C++20)
|
specifies that a relation imposes an equivalence relation (concept) |
(C++20)
|
determines if a sequence is a permutation of another sequence (niebloid) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/is_permutation