1. 需先排序,从小到大用next_permutation
float c[] = {1.2f, 32.2f, -1.3f};
std::sort(c, c + 3);
do
{
printf("%.2f %.2f %.2f\n", c[0], c[1], c[2]);
} while (std::next_permutation(c, c + 3));
2. 从大到小用prev_permutation
int b[] = {3, 2, 1};
do
{
printf("%d %d %d\n", b[0], b[1], b[2]);
} while (std::prev_permutation(b, b + 3));
3. 在全排列时,数组元素是被移动的,排序完毕后被归位
int a[] = {4, 3, 2, 1};
bool flag = std::next_permutation(a, a + 4);// 全部都从大到小了,返回false,全排列循环结束,序列复位(恢复为升序)
printf("flag = %d\n", flag);
for (int val : a)
printf("%d ", val);// 1 2 3 4
printf("\n");