STL_算法_排列(prev_permutationnext_permutation)
Posted 寻找星空的孩子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL_算法_排列(prev_permutationnext_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;
以上是关于STL_算法_排列(prev_permutationnext_permutation)的主要内容,如果未能解决你的问题,请参考以下文章