Namespaces
Variants

std:: is_permutation

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
is_permutation
(C++11)


C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
template < class ForwardIt1, class ForwardIt2 >

bool is_permutation ( ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2 ) ;
(1) (C++11부터)
(C++20부터 constexpr)
template < class ForwardIt1, class ForwardIt2,

class BinaryPredicate >
bool is_permutation ( ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2, BinaryPredicate p ) ;
(2) (C++11부터)
(C++20부터 constexpr)
template < class ForwardIt1, class ForwardIt2 >

bool is_permutation ( ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2, ForwardIt2 last2 ) ;
(3) (C++14부터)
(C++20부터 constexpr)
template < class ForwardIt1, class ForwardIt2,

class BinaryPredicate >
bool is_permutation ( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,

BinaryPredicate p ) ;
(4) (C++14부터)
(C++20부터 constexpr)

[ first1 , last1 ) 범위가 first2 에서 시작하는 범위의 permutation 인지 확인합니다:

  • 오버로드 (1,2) 의 경우, 두 번째 범위는 std:: distance ( first1, last1 ) 개의 요소를 가집니다.
  • 오버로드 (3,4) 의 경우, 두 번째 범위는 [ first2 , last2 ) 입니다.
1,3) 요소들은 operator == 를 사용하여 비교됩니다.
2,4) 요소들은 주어진 이항 조건자 p 를 사용하여 비교됩니다.

만약 ForwardIt1 ForwardIt2 가 서로 다른 value types 를 가지고 있다면, 프로그램은 ill-formed 상태입니다.

비교 함수가 동치 관계 가 아닌 경우, 동작은 정의되지 않습니다.

목차

매개변수

first1, last1 - 비교할 첫 번째 요소들의 범위 를 정의하는 반복자 쌍
first2, last2 - 비교할 두 번째 요소들의 범위 를 정의하는 반복자 쌍
p - 요소들이 동등하게 처리되어야 할 경우 ​ true 를 반환하는 이항 predicate.

predicate 함수의 시그니처는 다음에 해당해야 합니다:

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

시그니처에 const & 가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며, 값 범주 에 관계없이 (가능하다면 const인) Type1 Type2 타입의 모든 값을 수용할 수 있어야 합니다 (따라서 Type1 & 는 허용되지 않으며 , Type1 Type1 에 대해 이동이 복사와 동등하지 않는 한 허용되지 않습니다 (C++11부터) ).
Type1 Type2 타입은 InputIt1 InputIt2 타입의 객체가 역참조된 후 각각 Type1 Type2 로 암시적으로 변환될 수 있어야 합니다. ​

타입 요구사항
-
ForwardIt1, ForwardIt2 LegacyForwardIterator 요구사항을 충족해야 합니다.

반환값

true 범위 [ first1 , last1 ) 가 범위 [ first2 , last2 ) 의 순열인 경우, false 그렇지 않은 경우.

복잡도

주어진 N std:: distance ( first1, last1 ) 인 경우:

1) 두 범위가 동일한 경우 정확히 N 번의 비교를 operator == 로 수행하며, 그렇지 않은 경우 최악의 경우 O(N 2
)
번의 비교가 수행됩니다.
2) 정확히 N 번의 predicate p 적용은 두 범위가 동일한 경우에 해당하며, 그렇지 않을 경우 최악의 경우 O(N 2
)
번의 적용이 발생합니다.
3,4) 만약 ForwardIt1 ForwardIt2 가 모두 LegacyRandomAccessIterator 이고, last1 - first1 ! = last2 - first2 true 인 경우, 어떠한 비교도 수행되지 않습니다.
그렇지 않은 경우:
3) 두 범위가 동일한 경우 정확히 N 번의 비교를 operator == 로 수행하며, 그렇지 않은 경우 최악의 경우 O(N 2
)
번의 비교가 수행됩니다.
4) 두 범위가 동일한 경우 술어 p 를 정확히 N 번 적용하고, 그렇지 않은 경우 최악의 경우 O(N 2
)
번 적용합니다.

가능한 구현

template<class ForwardIt1, class ForwardIt2>
bool is_permutation(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 d_first)
{
    // 공통 접두사 건너뛰기
    std::tie(first, d_first) = std::mismatch(first, last, d_first);
    // 나머지 부분을 순회하며 [first, last)의 각 요소가
    // [d_first, d_last)에 몇 번 나타나는지 계산
    if (first != last)
    {
        ForwardIt2 d_last = std::next(d_first, std::distance(first, last));
        for (ForwardIt1 i = first; i != last; ++i)
        {
            if (i != std::find(first, i, *i))
                continue; // 이 *i는 이미 검사됨
            auto m = std::count(d_first, d_last, *i);
            if (m == 0 || std::count(i, last, *i) != m)
                return false;
        }
    }
    return true;
}

참고 사항

std::is_permutation 테스트 에 사용될 수 있으며, 즉 재배열 알고리즘(예: 정렬, 셔플링, 분할)의 정확성을 검사하는 데 사용됩니다. x 가 원본 범위이고 y 순열된 범위라면 std :: is_permutation ( x, y ) == true y "동일한" 요소들로 구성되어 있으며, 다른 위치에 있을 수 있음을 의미합니다.

예제

#include <algorithm>
#include <iostream>
template<typename Os, typename V>
Os& operator<<(Os& os, const V& v)
{
    os << "{ ";
    for (const auto& 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';
}

출력:

{ 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

참고 항목

요소 범위의 다음으로 큰 사전식 순열을 생성합니다
(function template)
요소 범위의 다음으로 작은 사전식 순열을 생성합니다
(function template)
relation 이 동치 관계를 부과함을 명시합니다
(concept)
한 시퀀스가 다른 시퀀스의 순열인지 확인합니다
(algorithm function object)