固然我们可以自己使用递归编写全排列程序,但是既然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. 【转】java获取当前路径的几种方法

    1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//use ...

  2. 如何在eclipse中添加android ADT

    百度经验:http://jingyan.baidu.com/article/b0b63dbfa9e0a74a4830701e.html 截图:

  3. OpenLayers中的Layer概念和实践--Openlayers调用WMS服务

    整理转自:http://hi.baidu.com/lixuweiok/item/c406a4e6a6d390e7fa42ba4b 本章我认为是这本书的真正开端,终于开始讲一些有意思的东西了.. 在这一 ...

  4. switch..case函数的基础使用一

    基本作用:switch中的参数与case的值进行比对,相等则进入case. JDK1.7 switch支持int.Integer.String类型 package com.my.test; impor ...

  5. MySQL 5.7原生JSON格式支持

    在MySQL与PostgreSQL的对比中,PG的JSON格式支持优势总是不断被拿来比较.其实早先MariaDB也有对非结构化的数据进行存储的方案,称为dynamic column,但是方案是通过BL ...

  6. Unity3D Development模式下的一个小问题

    今天客户提交了一个反馈,说测试版本的应用在按下电源键的时候屏幕变黑,然后重新按下电源键启动的时候发现没有出现屏幕锁屏的情况,直接回到应用界面. 我这边看了一下,发现如果装了360之类的手机助手就没这个 ...

  7. Unity3D 游戏开发构架篇 —— 动态大场景生成 = 区域加载+对象池管理

    项目做一个类似无尽模式的场景,想了一想,其实方法很简单,做一个相关的总结. 主要先谈一谈构架,后期附上代码. 一.区域加载 其实无尽场景的实现很简单,因为屏幕限制,那么不论何时何地,我们只能看到自己的 ...

  8. What is the difference between database table and database view?

    The database table has a physical existence in the database. A view is a virtual table, that is one ...

  9. java Hotspot 内存管理白皮书(中文翻译)

    转自: http://my.oschina.net/u/568779/blog/166891 1引言 一个健壮的 Java™2平台,Standard Edition (J2SE™)拥有一个自动内存管理 ...

  10. unity3d GameObject.Find 严格区分大小写的

    GameObject.Find 查找 static function Find (name : string) : GameObject Description描述 Finds a game obje ...