C++STL 之排列
固然我们可以自己使用递归编写全排列程序,但是既然STL里面已将有了这个功能为什么不直接用呢,下面就写一下直接使用C++ STL生成全排序的程序
函数名:next_permutation
包含头文件:algorithm
函数原型:
template<class BidirectionalIterator>
bool next_permutation(BidirectionalIterator _First, BidirectionalIterator _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 之排列的更多相关文章
- nyoj------擅长排列的小明
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序 ...
- POJ-2718
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12158 Accepted: 3 ...
- nyist oj 19 擅长排列的小明(dfs搜索+STL)
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 小明十分聪明.并且十分擅长排列计算.比方给小明一个数字5,他能立马给出1-5按字典序的全排列,假设你想 ...
- 枚举所有排列-STL中的next_permutation
枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 ...
- suseoj 1208: 排列问题 (STL, next_permutation(A.begin(), A.end()))
1208: 排列问题 时间限制: 1 Sec 内存限制: 128 MB提交: 2 解决: 2[提交][状态][讨论版][命题人:liyuansong] 题目描述 全排列的生成就是对于给定的字符集或 ...
- C++ STL next_permutation(快速排列组合)
排列组合必备!! https://blog.csdn.net/bengshakalakaka/article/details/78515480
- HDU 1027(数字排列 STL)
题意是求 n 个数在全排列中的第 m 个序列. 直接用 stl 中的 next_permutation(a, a+n) (这个函数是求一段序列的下一个序列的) 代码如下: #include <b ...
- hdu1716排列2(stl:next_permutation+优先队列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- poj 1833 排列 STL 全排列公式
排列 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15173 Accepted: 6148 Description 题 ...
随机推荐
- Windows Form 分页。
其实功能实现很简单.我做的是一个通用的分页控件.项目时间很紧,可能有点粗糙.欢迎大家斧正.不说了直接贴代码吧. using System; using System.Collections.Gener ...
- 使用go语言后的感受
前两天我说过为了学习go语言去学习了一遍python,当我完成了python的学习后,昨天中午就去学习了go语言.以下简称之为golang. 我用的操作系统是windows xp,golang对xp还 ...
- linux-CentOS6.4下安装oracle11g详解
参考地址:http://dengqsintyt.iteye.com/blog/1991930
- C# WPF打印报表
前天我的一个同学由于打印报表而苦恼,所以就介绍了一下WPF的打印报表,希望能帮助到大家. 展示报表 1. 首先新建项“报表”,选定项目,右击,点击“添加”->“新建项”->“报表”
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- struts2 ,web.xml中配置为/*.action,运行报错Invalid <url-pattern> /*.action in filter mapp
首先,修改成: <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/ ...
- 【tarjan】BZOJ 1051:受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3134 Solved: 1642[Submit][Sta ...
- C#根据日期DateTime和持续时间int找到日期
protected DateTime GetFinish(DateTime start, int duration) { return start.AddDays(duration); } prote ...
- UI框架说明
JQueryEasyUI jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要编 ...
- 当IIS挂的网站出现选 图片文件, 静态文件都打不开时, 可以试试新建一个应用程序池试试看...
当IIS挂的网站出现选 图片文件, 静态文件都打不开时, 可以试试新建一个应用程序池试试看...