티스토리 뷰

www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_permutation

 

next_permutation - C++ Reference

function template std::next_permutation default (1)template bool next_permutation (BidirectionalIterator first, BidirectionalIterator last); custom (2)template bool next_permutation (BidirectionalIterator first, BidirectionalIterator last, Comp

www.cplusplus.com

 

순열을 구할때 사용하는 함수.

 

#include <algorithm>

bool next_pertmutation(iterator first, iterator last)

 

[first, last) 의 원소를 다음 순열로 리어레인지하고 (lexicographically greater permutation) true를 반환.

다음 순열이 없다면 false를 반환.

 

순열을 나열할때는 다음과 같이 dowhile문을 활용

#include <iostream>
using namespace std;
#include <algorithm>

int main() {
    int d[4] = {1,2,3,4};
    do {
        for(int x : d) cout << x << ' ';
        cout << endl;
    } while(next_permutation(d, d+4));
}

결과:

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

 

prev_permutation은 동일하고, 이전 순열을 구함.

'노트' 카테고리의 다른 글

DFS를 이용한 순열 만들기  (0) 2021.06.28
Flask web 업로드용  (0) 2021.06.11
clion, 한글 주석시 컴파일 오류  (0) 2021.04.30
c++) 변수 크기의 배열 선언 (int arr[n])  (0) 2021.04.29
c++) 입출력 관련 속도  (0) 2021.04.14
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
글 보관함