ranges::next_permutation
ranges::next_permutation (다음 순열 — ranges)
범위를 사전순으로 다음 순열로 재배열하는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.
출처: cppreference
본문
std::ranges::next_permutation는 범위를 사전순으로 다음 순열로 바꿔요.
namespace std::ranges {
template< std::bidirectional_iterator I, std::sentinel_for<I> S,
class Comp = ranges::less, class Proj = std::identity >
requires std::sortable<I, Comp, Proj>
constexpr next_permutation_result<I>
next_permutation( I first, S last, Comp comp = {}, Proj proj = {} );
}
- 반환 타입
next_permutation_result{in, found}. found는 다음 순열로 성공적으로 변환되면true. 이미 마지막 순열이라 첫 순열로 되돌렸으면false.
std::vector<int> v{1, 2, 3};
do {
// 각 순열 처리
} while (std::ranges::next_permutation(v).found);
모든 순열을 사전순으로 탐색하는 ranges 버전이에요.