next_permutation
- 주어진 배열의 다음 순열을 구하는 함수이다.
- <algorithm> 라이브러리를 추가해 사용할 수 있다.
- 인자로 iterator를 두 개 받는데 보통 배열의 처음과 끝을 받는다.
- 다음 순열을 구할 수 있으면 true를 반환한 뒤 다음 순열로 재배열한다.
- 다음 순열을 구할 수 없으면 false를 반환한다.
#include <algorithm> vector<int> v = {1,2,3}; bool per = next_permutation(v.begin(), v.end());
사용 예
- 4자리 배열의 순열을 전부 구하고 싶을
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> v = { 0,1,2,3 }; do { for (auto i : v) { cout << i; } cout << "\n"; } while (next_permutation(v.begin(), v.end())); return 0; }