[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/algorithm/is_permutation [Back]  [Original]

std::is_permutation cppreference.com
cppreference.com

std::is_permutation

cppreference.com

<metanoindex/>

 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
(C++20)
, ranges::copy, ranges::sort, ...
(C++17)
(C++11)(C++11)(C++11)
(C++17)
(C++11)
( )
(C++11)
/
(C++11)
(C++17)

(C++17)
(C++17)
(C++17)
C
 
<tbody> </tbody>
template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );
(1) ( C++11)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, BinaryPredicate p );
(2) ( C++11)
true, [first1, last1), d_first. operator== , p
:
Returns true if there exists a permutation of the elements in the range [first1, last1) that makes that range equal to the range beginning at d_first. The first version uses operator== for equality, the second version uses the binary predicate p
Google.
. .

first, last
:
the range of elements to compare
Google.
. .
d_first
:
the beginning of the second range to compare
Google.
. .
p , true .

:

bool pred(const Type1 &a, const Type2 &b);

const &, .
Type1 Type2 , ForwardIt1 ForwardIt2 Type1 Type2 .

-
ForwardIt1, ForwardIt2 ForwardIterator.

true, [first, last) d_first.
:
true if the range [first, last) is a permutation of the range beginning at d_first.
Google.
. .

O(N2) , N , N=std::distance(first, last).
:
At most O(N2) applications of the predicate, or exactly N if the sequences are already equal, where N=std::distance(first, last).
Google.
. .

template<class ForwardIt1, class ForwardIt2>
bool is_permutation(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 d_first)
{
   // skip common prefix
   std::tie(first, d_first) = std::mismatch(first, last, d_first);
   // iterate over the rest, counting how many times each element
   // from [first, last) appears in [d_first, d_last)
   if (first != last) {
       ForwardIt2 d_last = d_first;
       std::advance(d_last, std::distance(first, last));
       for (ForwardIt1 i = first; i != last; ++i) {
            if (i != std::find(first, i, *i)) continue; // already counted this *i

            auto m = std::count(d_first, d_last, *i);
            if (m==0 || std::count(i, last, *i) != m) {
                return false;
            }
        }
    }
    return true;
}

#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';
}

:

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

.


( ) []

( ) []

Web Proxy Viewer  |  New URL  |  Original Page