algorithm_is_permutation

algorithm_is_permutation (순열 관계 검사)

std::is_permutation는 두 범위가 서로 같은 요소를 같은 개수로 담고 있는지(즉 하나가 다른 하나의 순열인지) 검사해요.

출처: cppreference

본문

std::is_permutation[first1, last1)first2에서 시작하는 범위의 순열인지 검사해요. <algorithm> 헤더에 정의되어 있어요.

template< class ForwardIt1, class ForwardIt2 >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2 );

template< class ForwardIt1, class ForwardIt2,
          class BinaryPredicate >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2, BinaryPredicate p );

template< class ForwardIt1, class ForwardIt2 >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2, ForwardIt2 last2 );
  • 1,2번 오버로드 — 두 번째 범위는 std::distance(first1, last1)개의 요소를 가져요.
  • 3,4번 오버로드 — 두 번째 범위는 [first2, last2)예요.
  • 요소는 operator==(또는 이진 술어 p)로 비교돼요.

ForwardIt1ForwardIt2의 값 타입이 다르면 프로그램은 ill-formed예요.

반환값 (Return value)

첫 번째 범위가 두 번째 범위의 순열이면 true, 아니면 false예요.

복잡도 (Complexity)

최대 𝓞(N²)번의 비교가 필요해요. 두 범위가 같은 요소로 시작한다면 검사 없이 즉시 true일 수 있어요.

예제 (Example)

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v1{1, 2, 3, 4, 5};
    std::vector<int> v2{3, 5, 4, 1, 2};
    std::vector<int> v3{3, 5, 4, 1, 1};

    std::cout << std::boolalpha << std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n';
    std::cout << std::boolalpha << std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n';
}

출력:

true
false

더 알아보기 (Learn more)

cppreference