STL_算法_排列(prev_permutation、next_permutation)
C++ Primer 学习中。。。
简单记录下我的学习过程 (代码为主)
//全部容器适用
next_permutation(b,e) //下一个排列-----从小到大 返回值false,表示没有下一个
next_permutation(b,e,cp)
prev_permutation(b,e) //上一个排列-----从大到小 返回值true,表示还有下一个
prev_permutation(b,e,cp)
/**------http://blog.csdn.net/u010579068------**/ #include<iostream> #include<cstdio> #include<string> #include<vector> #include<list> #include<deque> #include<algorithm> using namespace std; /***************************************** //全部容器适用 next_permutation(b,e) //下一个排列-----从小到大 返回值false,表示没有下一个 next_permutation(b,e,cp) prev_permutation(b,e) //上一个排列-----从大到小 返回值true,表示还有下一个 prev_permutation(b,e,cp) *****************************************/ /**---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------**/ /************************************************************************************* std::next_permutation 全部排序容器适用 algorithm -------------------------------------------------------------------------------------- template <class BidirectionalIterator> bool next_permutation (BidirectionalIterator first, BidirectionalIterator last ); template <class BidirectionalIterator, class Compare> bool next_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp); //eg: *************************************************************************************/ /************************************************************************************* std::prev_permutation 全部排序容器适用 algorithm -------------------------------------------------------------------------------------- template <class BidirectionalIterator> bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last ); template <class BidirectionalIterator, class Compare> bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp); //eg: *************************************************************************************/ int myints[3]; void init() { myints[0]=2; myints[1]=1; myints[2]=3; } void init2() { myints[0]=3; myints[1]=2; myints[2]=3; } int main() { // next_permutation init(); cout << "The 3! possible permutations with 3 elements:\n"; sort (myints,myints+3);//从小到大 do { cout << myints[0] << " " << myints[1] << " " << myints[2] << endl; } while ( next_permutation (myints,myints+3) ); // while(next_permutation(myints,myints+3)) // cout << myints[0] << " " << myints[1] << " " << myints[2] << endl; init(); cout << "The 3! possible permutations with 3 elements:\n"; sort (myints,myints+3); reverse (myints,myints+3);//从大到小 do { cout << myints[0] << " " << myints[1] << " " << myints[2] << endl; } while ( prev_permutation (myints,myints+3) ); cout<<"--------------------------------------------------------"<<endl; init2(); cout << "The 3! possible permutations with 3 elements:\n"; sort (myints,myints+3); do { cout << myints[0] << " " << myints[1] << " " << myints[2] << endl; } while ( next_permutation (myints,myints+3) ); init2(); cout << "The 3! possible permutations with 3 elements:\n"; sort (myints,myints+3); reverse (myints,myints+3);//从大到小 do { cout << myints[0] << " " << myints[1] << " " << myints[2] << endl; } while ( prev_permutation (myints,myints+3) ); return 0; }