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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值