[C++] 순열

xyzw·2025년 10월 31일
0

C++

목록 보기
12/12

다음 순열

bool next_permutation(Iterator first, Iterator last)

  • 현재 배열이나 벡터의 순서를 기준으로 다음 순서의 순열로 재배열
  • 다음 순열이 있으면 true, 없으면 false 반환
vector<int> v = {2, 3, 1};
next_permutation(v.begin(), v.end());
for (int x : v) cout << x << ' ';   // 출력: 3 1 2

이전 순열

bool prev_permutation(Iterator first, Iterator last)

  • 현재 배열이나 벡터의 순서를 기준으로 이전 순서의 순열로 재배열
  • 다음 순열이 있으면 true, 없으면 false 반환
vector<int> v = {2, 3, 1};
prev_permutation(v.begin(), v.end());
for (int x : v) cout << x << ' ';   // 출력: 2 1 3

모든 순열 탐색

vector<int> v = { ... };  // 오름차순 정렬된 벡터

do {
	// 현재 순서를 이용한 연산 수행
} while(next_permutation(v.begin(), v.end());

0개의 댓글