STL的排列算法next_permutation

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//定义一个测试数组
void TestArray()
{
   cout << "TestArray is :" << endl;
   int b[] = {3, 2, 1};
   int count = sizeof(b) / sizeof(int);
   //因为next_permutation(a, a + count)从当前序列开始,只输出比它大的数列
   for(int i = 0; i < count; ++i)
   {
      cout << b[i] << " ";
   }
    cout << endl;
   next_permutation(b, b + count);
   do
   {
      for(int i = 0; i < count ; ++i)
       {
           //cout << a[i] << " ";
           cout << b[i] << " ";
       }
       cout << endl;
   }while(next_permutation(b, b + count));
   cout << endl;
}
//创建一个vector用来存放数组
//如果当前序列已经是按字典排序的最后一个序列,则next_permutation将序列排列为最低排列并返回false
//否则将输入序列变换为下一个排列,机子点的下一个排列,并返回true(此时不输出原序列)
void TestVector()
{
    cout << "TestVector is:" << endl;
    char a[] = {'c', 'b', 'a'};
    int count = sizeof(a) / sizeof(char);
    vector<char> vec(a, a+count);
    for(vector<char>::iterator itr = vec.begin(); itr != vec.end(); ++itr)
        {
            cout << *itr << " ";
        }
    cout << endl;
    next_permutation(vec.begin(), vec.end());
    do
    {
        for(vector<char>::iterator itr = vec.begin(); itr != vec.end(); ++itr)
        {
            cout << *itr << " ";
        }
        cout << endl;
    }while(next_permutation(vec.begin(), vec.end()));
   cout << endl;
}
int main()
{
    TestArray();
    TestVector();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值