std:: is_permutation
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
ForwardIt1,
class
ForwardIt2
>
bool
is_permutation
(
ForwardIt1 first1, ForwardIt1 last1,
|
(1) |
(C++11부터)
(C++20부터 constexpr) |
|
template
<
class
ForwardIt1,
class
ForwardIt2,
class
BinaryPredicate
>
|
(2) |
(C++11부터)
(C++20부터 constexpr) |
|
template
<
class
ForwardIt1,
class
ForwardIt2
>
bool
is_permutation
(
ForwardIt1 first1, ForwardIt1 last1,
|
(3) |
(C++14부터)
(C++20부터 constexpr) |
|
template
<
class
ForwardIt1,
class
ForwardIt2,
class
BinaryPredicate
>
|
(4) |
(C++14부터)
(C++20부터 constexpr) |
[
first1
,
last1
)
범위가
first2
에서 시작하는 범위의
permutation
인지 확인합니다:
- 오버로드 (1,2) 의 경우, 두 번째 범위는 std:: distance ( first1, last1 ) 개의 요소를 가집니다.
-
오버로드
(3,4)
의 경우, 두 번째 범위는
[first2,last2)입니다.
만약
ForwardIt1
과
ForwardIt2
가 서로 다른
value types
를 가지고 있다면, 프로그램은 ill-formed 상태입니다.
비교 함수가 동치 관계 가 아닌 경우, 동작은 정의되지 않습니다.
목차 |
매개변수
| first1, last1 | - | 비교할 첫 번째 요소들의 범위 를 정의하는 반복자 쌍 |
| first2, last2 | - | 비교할 두 번째 요소들의 범위 를 정의하는 반복자 쌍 |
| p | - |
요소들이 동등하게 처리되어야 할 경우
true
를 반환하는 이항 predicate.
predicate 함수의 시그니처는 다음에 해당해야 합니다: bool pred ( const Type1 & a, const Type2 & b ) ;
시그니처에
const
&
가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며,
값 범주
에 관계없이 (가능하다면 const인)
|
| 타입 요구사항 | ||
-
ForwardIt1, ForwardIt2
는
LegacyForwardIterator
요구사항을 충족해야 합니다.
|
||
반환값
true
범위
[
first1
,
last1
)
가 범위
[
first2
,
last2
)
의 순열인 경우,
false
그렇지 않은 경우.
복잡도
주어진 N 이 std:: distance ( first1, last1 ) 인 경우:
) 번의 비교가 수행됩니다.
) 번의 적용이 발생합니다.
ForwardIt1
과
ForwardIt2
가 모두
LegacyRandomAccessIterator
이고,
last1
-
first1
!
=
last2
-
first2
가
true
인 경우, 어떠한 비교도 수행되지 않습니다.
) 번의 비교가 수행됩니다.
) 번 적용합니다.
가능한 구현
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) |
|
|
(C++20)
|
relation
이 동치 관계를 부과함을 명시합니다
(concept) |
|
(C++20)
|
한 시퀀스가 다른 시퀀스의 순열인지 확인합니다
(algorithm function object) |