Namespaces
Variants

std:: next_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
next_permutation

C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
template < class BidirIt >
bool next_permutation ( BidirIt first, BidirIt last ) ;
(1) (C++20부터 constexpr)
template < class BidirIt, class Compare >
bool next_permutation ( BidirIt first, BidirIt last, Compare comp ) ;
(2) (C++20부터 constexpr)

범위 [ first , last ) 를 다음 순열 로 재배열합니다. "다음 순열"이 존재할 경우 true 를 반환하고, 그렇지 않을 경우 범위를 사전식 첫 번째 순열로 변환한 후( std::sort 를 사용한 것처럼) false 를 반환합니다.

1) 모든 순열의 집합은 사전식 순서로 정렬됩니다. 이때 순서 기준은 operator < (C++20 이전) std:: less { } (C++20 이후) 입니다.
2) 모든 순열의 집합은 comp 에 대해 사전식 순서로 정렬됩니다.

만약 * first 의 타입이 Swappable 가 아닌 경우 (C++11 이전) BidirIt ValueSwappable 가 아닌 경우 (C++11 이후) , 동작은 정의되지 않습니다.

목차

매개변수

first, last - 순열을 생성할 요소들의 범위 를 정의하는 반복자 쌍
comp - 비교 함수 객체(즉 Compare 요구 사항을 만족하는 객체)로 첫 번째 인수가 두 번째 인수보다 작은 경우 true 를 반환함.

비교 함수의 시그니처는 다음과 동일해야 함:

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

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

타입 요구 사항
-
BidirIt LegacyBidirectionalIterator 의 요구 사항을 충족해야 함.

반환값

true 새로운 순열이 이전 순열보다 사전식 순서에서 더 큰 경우. false 마지막 순열에 도달했고 범위가 첫 번째 순열로 재설정된 경우.

복잡도

주어진 N std:: distance ( first, last ) 인 경우:

1,2) 최대
N
2
회의 스왑.

예외

반복자 연산 또는 요소 교체에서 발생하는 모든 예외.

가능한 구현

template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last)
{
    auto r_first = std::make_reverse_iterator(last);
    auto r_last = std::make_reverse_iterator(first);
    auto left = std::is_sorted_until(r_first, r_last);
    if (left != r_last)
    {
        auto right = std::upper_bound(r_first, left, *left);
        std::iter_swap(left, right);
    }
    std::reverse(left.base(), last);
    return left != r_last;
}

참고 사항

전체 순열 시퀀스에 걸쳐 평균적으로, 일반적인 구현은 호출당 약 3회의 비교와 1.5회의 스왑을 사용합니다.

구현체들(예: MSVC STL )은 반복자 타입이 LegacyContiguousIterator 요구사항을 만족하고 해당 값 타입의 스왑 연산이 비트리비얼 특수 멤버 함수를 호출하지 않으며 ADL 에서 발견된 swap 을 호출하지 않을 때 벡터화를 활성화할 수 있습니다.

예제

다음 코드는 문자열 "aba" 의 세 가지 순열을 모두 출력합니다.

#include <algorithm>
#include <iostream>
#include <string>
int main()
{
    std::string s = "aba";
    do
    {
        std::cout << s << '\n';
    }
    while (std::next_permutation(s.begin(), s.end()));
    std::cout << s << '\n';
}

출력:

aba
baa
aab

참고 항목

시퀀스가 다른 시퀀스의 순열인지 판단합니다
(함수 템플릿)
요소 범위의 다음으로 작은 사전식 순열을 생성합니다
(함수 템플릿)
요소 범위의 다음으로 큰 사전식 순열을 생성합니다
(알고리즘 함수 객체)