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; }
![[STL] 11. next_permutation](/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fattachment%253A99e3ee62-7b1c-452a-be86-c17e5d28203f%253ASTL.png%3Ftable%3Dblock%26id%3D1f887e7f-a582-8160-8585-d9ce144bab2a%26cache%3Dv2&w=3840&q=75)