next_permutation
时间: 2023-08-30 16:09:55 浏览: 37
`next_permutation` is a function in C++ that is used to generate the next lexicographically greater permutation of a given sequence. It rearranges the elements of the sequence such that the next permutation is generated. If the given sequence is already the largest possible permutation, then it rearranges the sequence to the smallest possible permutation.
Syntax:
```c++
template <class BidirectionalIterator>
bool next_permutation (BidirectionalIterator first, BidirectionalIterator last);
```
Parameters:
- `first`: Iterator to the first element of the sequence.
- `last`: Iterator to the last element of the sequence.
Return value:
- `true` if the next lexicographically greater permutation exists, otherwise `false`.
Example usage:
```c++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {1, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);
do {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
} while (next_permutation(arr, arr + n));
return 0;
}
```
Output:
```
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
```