固然我们可以自己使用递归编写全排列程序,但是既然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. 敏捷开发之道(二)极限编程XP

    上次的博文敏捷开发之道(一)敏捷开发宣言中,我们介绍了一下敏捷开发宣言,在其中,我们了解到了关于敏捷开发的几个重要的价值观.今天我们来了解一个敏捷开发的方法--极限编程XP 1.介绍 极限编程(eXt ...

  2. JPages分页插件的使用

    废话不多说,直接上代码. 首先下载JPages的js和css包,附上下载地址:http://dl.oschina.net/softfile/jpages/jpages-latest-138554713 ...

  3. Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心

    题目链接: 题目 D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  4. 20160727noip模拟赛zld

    首先最优策略肯定是这样的:我们取出这个序列中的最大值,然后将整个序列分为左右两部分, 那么我们一定先把左右两部分合起来然后再与这个值合并 那么我们可以得出一个基于最值查询(rmq)的的算法,但是zld ...

  5. PAT-乙级-1048. 数字加密(20)

    1048. 数字加密(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求实现一种数字加密方法.首先固 ...

  6. 【WCF--初入江湖】11 安全

    11 安全 前言 [1]传输安全  传输安全模式  传输安全与绑定协议   [2]身份验证  身份验证分类  证书  示例:传输安全匿名客户端证书的使用 1. 传输安全     保证信息在传输过程中的 ...

  7. asp.net mvc4 使用KindEditor文本编辑器

    最近做项目要用文本编辑器,编辑器好多种,这里介绍KindEditor在asp.net mvc4中的使用方法. 一.准备工作: 1.下载KindEditor.去官网:http://www.kindsof ...

  8. Unity3d Android程序嵌入Admob广告条

    原地址:http://dong2008hong.blog.163.com/blog/static/4696882720140441353482/ Seems like using a simple A ...

  9. linux源代码阅读笔记 linux文件系统(三)

    当系统申请一个新的inode时.系统并不会对磁盘进行读写.它会在存储在内存的inode表(inode_table)中寻找一个空闲的位置. 如果找到了,直接返回该inode.否则要等待一个空闲的位置. ...

  10. pycharm 基础教程

    pycharm 教程(一)安装和首次使用 PyCharm 是我用过的python编辑器中,比较顺手的一个.而且可以跨平台,在macos和windows下面都可以用,这点比较好. 首先预览一下 PyCh ...