function template
<algorithm>
std::is_permutation equality (1)template <class ForwardIterator1, class ForwardIterator2> bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate pred);
Test whether range is permutation of another
Compares the elements in the range[first1,last1)
with those in the range beginning at first2, and returns true
if all of the elements in both ranges match, even in a different order.
The elements are compared using operator==
(or pred, in version (2)).
The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class InputIterator1, class InputIterator2>
bool is_permutation (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2)
{
std::tie (first1,first2) = std::mismatch (first1,last1,first2);
if (first1==last1) return true;
InputIterator2 last2 = first2; std::advance (last2,std::distance(first1,last1));
for (InputIterator1 it1=first1; it1!=last1; ++it1) {
if (std::find(first1,it1,*it1)==it1) {
auto n = std::count (first2,last2,*it1);
if (n==0 || std::count (it1,last1,*it1)!=n) return false;
}
}
return true;
}
[first1,last1)
, which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
[first1,last1)
.
bool
. The value returned indicates whether the elements are considered to match in the context of this function.
true
if all the elements in the range [first1,last1)
compare equal to those of the range starting at first2 in any order, and false
otherwise.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// is_permutation example
#include <iostream> // std::cout
#include <algorithm> // std::is_permutation
#include <array> // std::array
int main () {
std::array<int,5> foo = {1,2,3,4,5};
std::array<int,5> bar = {3,1,4,5,2};
if ( std::is_permutation (foo.begin(), foo.end(), bar.begin()) )
std::cout << "foo and bar contain the same elements.\n";
return 0;
}
foo and bar contain the same elements.
N2
element comparisons until the result is determined (where N is the distance between first1 and last1).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4