Defined in header <algorithm> | ||||
---|---|---|---|---|
| (1) | (since C++11) | ||
| (2) | (since C++11) | ||
| (3) | (since C++14) | ||
| (4) | (since C++14) |
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.
The overloads (1) and (3) use operator==
for equality, whereas the overloads (2) and (4) use the binary predicate p
.
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 ForwardIterator . | ||
- 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 RandomAccessIterator
and std::distance(first1, last1) != std::distance(first2, last2)
no applications of the predicate are made.
Possible implementation
|
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #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:
1 2 | 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) |
Please login to continue.