STL之next_permutation——求全排列

next_permutation

功能:将[first, last)范围内的排列重组为字典序更大的下一个新排列。permutation正是“排列”之意。

调用形式:next_permutation(first, last),其中,first是指向排列头元素的指针,last是指向排列末元素再下一位的指针,两者给出排列范围:[first, last).

函数所在头文件:<algorithm>

例子:

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int a[3] = {1, 2, 3};
 8     cout << "关于数组a的所有3!种排列按字典序升序排列为:" << endl;
 9     do
10     {
11         cout << a[0] << ' ' << a[1] << ' ' << a[2] << endl;
12     }while(next_permutation(a, a + 3));
13     return 0;
14 }

运行结果:

关于数组a的所有3!种排列按字典序升序排列为:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

类似函数:prev_permutation,功能是求给定排列的下一个字典序更大的排列。

 

原文地址:https://www.cnblogs.com/cszlg/p/2975859.html