On this page
std::ranges::is_permutation
Defined in header <algorithm> |
||
|---|---|---|
| Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
true if there exists a permutation of the elements in range [first1, last1) that makes the range equal to [first2, last2) (after application of corresponding projections Proj1, Proj2, and using the binary predicate Pred as a comparator). Otherwise returns false.
r1 as the first source range and r2 as the second source range, as if using ranges::begin(r1) as first1, ranges::end(r1) as last1, ranges::begin(r2) as first2, and ranges::end(r2) as last2.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
Parameters
| first1, last1 | - | the first range of the elements |
| first2, last2 | - | the second range of the elements |
| r1 | - | the first range of the elements |
| r2 | - | the second range of the elements |
| pred | - | predicate to apply to the projected elements |
| proj1 | - | projection to apply to the elements in the first range |
| proj2 | - | projection to apply to the elements in the second range |
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 and each projection, or exactly \(\scriptsize N\)N if the sequences are already equal, where \(\scriptsize N\)N is ranges::distance(first1, last1). However if ranges::distance(first1, last1) != ranges::distance(first2, last2), no applications of the predicate and projections are made.
Notes
The permutation relation is an equivalence relation.
The ranges::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 ranges::is_permutation(x, y) == true means that y consist of "the same" elements, maybe staying at other positions.
Possible implementation
|
Example
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <ranges>
auto& operator<<(auto& os, std::ranges::forward_range auto const& v)
{
os << "{ ";
for (auto const& e : v) os << e << ' ';
return os << "}";
}
int main()
{
static constexpr auto r1 = {1, 2, 3, 4, 5};
static constexpr auto r2 = {3, 5, 4, 1, 2};
static constexpr auto r3 = {3, 5, 4, 1, 1};
static_assert(
std::ranges::is_permutation(r1, r1) &&
std::ranges::is_permutation(r1, r2) &&
std::ranges::is_permutation(r2, r1) &&
std::ranges::is_permutation(r1.begin(), r1.end(), r2.begin(), r2.end()));
std::cout
<< std::boolalpha
<< "is_permutation( " << r1 << ", " << r2 << " ): "
<< std::ranges::is_permutation(r1, r2) << '\n'
<< "is_permutation( " << r1 << ", " << r3 << " ): "
<< std::ranges::is_permutation(r1, r3) << '\n'
<< "is_permutation with custom predicate and projections: "
<< std::ranges::is_permutation(
std::array {-14, -11, -13, -15, -12}, // 1st range
std::array {'F', 'E', 'C', 'B', 'D'}, // 2nd range
[](int x, int y) { return abs(x) == abs(y); }, // predicate
[](int x) { return x + 10; }, // projection for 1st range
[](char y) { return int(y - 'A'); }) // projection for 2nd range
<< '\n';
}
Output:
is_permutation( { 1 2 3 4 5 }, { 3 5 4 1 2 } ): true
is_permutation( { 1 2 3 4 5 }, { 3 5 4 1 1 } ): false
is_permutation with custom predicate and projections: true
See also
|
(C++20)
|
generates the next greater lexicographic permutation of a range of elements (niebloid) |
|
(C++20)
|
generates the next smaller lexicographic permutation of a range of elements (niebloid) |
|
(C++11)
|
determines if a sequence is a permutation of another sequence (function template) |
| 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) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/is_permutation