C++ STL 入门 全排列函数
全排列函数 next_permutation
STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题)
头文件:
#include <algorithm>
using namespace std;
调用:next_permutation(start, end);
注意:函数要求输入的是一个升序排列的序列的头指针和尾指针.
用法:
// 数组用法
void use_next_permutation_Array() {
int a[N];
for(int i=0; i<N; i++) {
a[i] = i;
}
do {
for (int i=0; i<N; i++)
cout << a[i] << " ";
cout << endl;
} while(next_permutation(a, a+N));
}
// 向量用法
void use_next_permutation_Vector(){
vector<int> int_vec;
for (int i=0; i<N; i++){
int_vec.push_back(rand() % N);
}
sort(int_vec.begin(), int_vec.end());
do{
for (vector<int>::iterator it = int_vec.begin();
it != int_vec.end(); it ++){
cout << *it << " " ;
}
cout << endl;
}while(next_permutation(int_vec.begin(), int_vec.end()));
}