next_permutation
时间: 2023-08-30 22:09:55 浏览: 40
next_permutation is a function in the C++ STL (Standard Template Library) that generates the next lexicographically greater permutation of a sequence. It modifies the sequence in place, and if there is no greater permutation, it returns false.
Here is an example usage of next_permutation:
```
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3};
do {
for (int i : v) {
std::cout << i << ' ';
}
std::cout << '\n';
} while (std::next_permutation(v.begin(), v.end()));
return 0;
}
```
This program generates all permutations of the sequence {1, 2, 3}:
```
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
```
The function std::next_permutation is called in the while loop condition to generate the next permutation. The loop continues until there is no greater permutation left.