next_permutation()
prev_permutation()
#include<iostream> #include<algorithm> #include<vector> // 排列组合开始之前一定要先排序 using namespace std; int main() { vector<int> ivec; ivec.push_back(1); ivec.push_back(2); ivec.push_back(3); for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) cout << *iter << ' '; cout << endl; //下一个排列组合 该算法返回值为bool 若返回值为true 说明还有下一个排列组合,但是若为fals 说明排列组合已经排列完了 next_permutation(ivec.begin(), ivec.end()); for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) cout << *iter << ' '; cout << endl; while (next_permutation(ivec.begin(), ivec.end())) { for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++) cout << *iter << ' '; cout << endl; } cout << endl << endl; cout << endl << endl; cout << endl << endl; vector<int> ivec2; ivec2.push_back(3); ivec2.push_back(2); ivec2.push_back(1); for (vector<int>::iterator iter = ivec2.begin(); iter != ivec2.end(); iter++) cout << *iter; ' '; cout << endl; //ivec2 本身是个倒序 while (prev_permutation(ivec2.begin(), ivec2.end())) { for (vector<int>::iterator iter = ivec2.begin(); iter != ivec2.end(); iter++) cout << *iter; ' '; cout << endl; } // system("pause"); return 0; }
时间: 2024-10-11 02:46:48