固然我们可以自己使用递归编写全排列程序,但是既然STL里面已将有了这个功能为什么不直接用呢,下面就写一下直接使用C++ STL生成全排序的程序

函数名:next_permutation

包含头文件:algorithm

函数原型:

template<class BidirectionalIterator>   

bool next_permutation(BidirectionalIterator _FirstBidirectionalIterator _Last    );

template<class BidirectionalIterator, class BinaryPredicate>   

bool next_permutation(BidirectionalIterator _First,  BidirectionalIterator _Last, BinaryPredicate _Comp    );

两个重载函数,第二个带谓词参数_Comp,其中只带两个参数的版本,默认谓词函数为"小于".

返回值:bool类型(默认若当前调用排列到达最大字典序则返回false,同时重新设置该排列为最小字典序,否则返回true,并按照字典递增的顺序输出下一个排列。例如,在字母表中,abcd的下一单词排列为abdc)

所以如果是生成一个数组的全排列,先要对数组按升序排序,然后使用do-while语句循环调用next_permutation函数

 #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
int len=str.length();
char *cstr=(char *)str.c_str();
cout<<"排列输出如下"<<endl;
do
{
cout<<cstr<<endl;
}while(next_permutation(cstr,cstr+len));
cout<<"排列之后cstr变为:"<<endl;
cout<<cstr;
return ;
}

上面是一个没有加排序直接调用nextpermation看一下,不同输入的情况下输出结果的比较

 #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
int len=str.length();
char *cstr=(char *)str.c_str();
sort(cstr,cstr+len);
cout<<"排列输出如下"<<endl;
do
{
cout<<cstr<<endl;
}while(next_permutation(cstr,cstr+len));
cout<<"排列之后cstr变为:"<<endl;
cout<<cstr;
return ;
}

加上排序之后,看看效果

C++STL 之排列的更多相关文章

  1. nyoj------擅长排列的小明

    擅长排列的小明 时间限制:1000 ms  |           内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序 ...

  2. POJ-2718

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 3 ...

  3. nyist oj 19 擅长排列的小明(dfs搜索+STL)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 小明十分聪明.并且十分擅长排列计算.比方给小明一个数字5,他能立马给出1-5按字典序的全排列,假设你想 ...

  4. 枚举所有排列-STL中的next_permutation

    枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 ...

  5. suseoj 1208: 排列问题 (STL, next_permutation(A.begin(), A.end()))

    1208: 排列问题 时间限制: 1 Sec  内存限制: 128 MB提交: 2  解决: 2[提交][状态][讨论版][命题人:liyuansong] 题目描述 全排列的生成就是对于给定的字符集或 ...

  6. C++ STL next_permutation(快速排列组合)

    排列组合必备!! https://blog.csdn.net/bengshakalakaka/article/details/78515480

  7. HDU 1027(数字排列 STL)

    题意是求 n 个数在全排列中的第 m 个序列. 直接用 stl 中的 next_permutation(a, a+n) (这个函数是求一段序列的下一个序列的) 代码如下: #include <b ...

  8. hdu1716排列2(stl:next_permutation+优先队列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  9. poj 1833 排列 STL 全排列公式

    排列 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15173   Accepted: 6148 Description 题 ...

随机推荐

  1. iOS - 指定视图的圆角个数-b

    平常设置视图的圆角最普遍的就是设置四个角的,方法也就是一句代码解决: view.layer.cornerRadius = 10; 四个圆角 但有时需求会是指定某个,或者特定哪几个角设置圆角,所以我们需 ...

  2. android 开发不能创建目录

    原来代码: File tempDir = new File(path); //path 是一个参数 if (!tempDir.exists()) { try { tempDir.mkdir(); // ...

  3. Timer 的缺陷

    java.util.Timer计时器有管理任务延迟执行("如1000ms后执行任务")以及周期性执行("如每500ms执行一次该任务").但是,Timer存在一 ...

  4. Windows Phone 8.1 与 Windows Phone 8.1 Silverlight区别

    以下讨论基于当前的WP8 APP, 一.Windows Phone 8 app:旧的WP8程序:不需要迁移: 二.Windows Phone 8.1 app新的使用Windows Runtime 的程 ...

  5. 【BZOJ】【1662】/【POJ】【3252】 【USACO 2006 Nov】Round Number

    数位DP 同上一题Windy数 预处理求个组合数 然后同样的方法,这次是记录一下0和1的个数然后搞搞 Orz cxlove /************************************* ...

  6. time wait duo

    linux 下Time_wait过多问题解决 分类: linux FAQ2011-07-14 11:20 3485人阅读 评论(0) 收藏 举报 linux服务器tcp通讯活动ssh 问题起因: 自己 ...

  7. recursion lead to out of memory

    There are two storage areas involved: the stack and the heap. The stack is where the current state o ...

  8. 【leetcode】Median of Two Sorted Arrays(hard)★!!

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  9. php集成开发环境的安装以及Zend Studio开发工具的安装

    一.集成开发环境: wampserver 下载地址: 官网: http://www.wampserver.com/ 直接下载 http://sourceforge.net/projects/wamps ...

  10. JavaC 编译目录下所有的UTF-8编码的java文件

    javac -encoding UTF-8  *.java