这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>
下面是以前的笔记    与之完全相反的函数还有prev_permutation
 
 
(1) int 类型的next_permutation
 
int main()
{
 int a[3];
a[0]=1;a[1]=2;a[2]=3;
 do
{
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
} while (next_permutation(a,a+3)); //参数3指的是要进行排列的长度
 
//如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继
 
 
}
 
输出:
 
 1 2 3
 1 3 2
 2 1 3
 2 3 1
 3 1 2
 3 2 1
 
 
如果改成 while(next_permutation(a,a+2));
则输出:
 1 2 3
 2 1 3
 
只对前两个元素进行字典排序
显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3
 
 
 
若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
 
 int list[3]={3,2,1};
next_permutation(list,list+3);
cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;
 
//输出: 1 2 3

(2) char 类型的next_permutation
 
int main()
{
 char ch[205];
cin >> ch;
 
sort(ch, ch + strlen(ch) );
//该语句对输入的数组进行字典升序排序。如输入9874563102 cout<<ch; 将输出0123456789,这样就能输出全排列了
 
 char *first = ch;
 char *last = ch + strlen(ch);
 
 do {
cout<< ch << endl;
}while(next_permutation(first, last));
 return 0;
}
 
//这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序
//若采用 while(next_permutation(ch,ch+5)); 如果只输入1562,就会产生错误,因为ch中第五个元素指向未知
//若要整个字符串进行排序,参数5指的是数组的长度,不含结束符

(3) string 类型的next_permutation
 
int main()
{
 string line;
 while(cin>>line&&line!="#")
{
 if(next_permutation(line.begin(),line.end())) //从当前输入位置开始
cout<<line<<endl;
 else cout<<"Nosuccesor\n";
}
}
 
 
 
int main()
{
 string line;
 while(cin>>line&&line!="#")
{
sort(line.begin(),line.end());//全排列
cout<<line<<endl;
 while(next_permutation(line.begin(),line.end()))
cout<<line<<endl;
}
}
 
 
 
 
 
 
 next_permutation 自定义比较函数
 
 
#include<iostream> //poj 1256 Anagram
#include<string>
#include<algorithm>
using namespace std;
int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.
{
 if(tolower(a)!=tolower(b))
 return tolower(a)<tolower(b);
 else
 return a<b;
}
int main()
{
 char ch[20];
 int n;
cin>>n;
 while(n--)
{
scanf("%s",ch);
sort(ch,ch+strlen(ch),cmp);
 do
{
printf("%s\n",ch);
}while(next_permutation(ch,ch+strlen(ch),cmp));
}
 return 0;
}

next_permutation函数相关推荐

  1. c语言next函数,详谈全排列next_permutation() 函数的用法(推荐)

    这是一个c++函数,包含在头文件里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下面的代码可产生1~n的全排列 #i ...

  2. next_permutation函数与perv_permutation函数

    next_permutation函数与perv_permutation函数声明:#include  <algorithm> 注意:两个函数初始分别为1,2,3 3,2,1否有不同的输出 n ...

  3. 打印数组的排列组合/STL next_permutation函数

    在STL中,除了next_permutation外,还有一个函数prev_permutation,两者都是用来计算排列组合的函数. https://blog.csdn.net/qq_42391248/ ...

  4. C++STL之next_permutation()函数使用

    如n==3时的全排列为 123 132 213 231 312 321 代码如下: #include #include using namespace std; int main(){ int a[1 ...

  5. 【ACM】与全排列相关的STL函数 prev_permutation next_permutation

    排列  与  全排列 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列. 当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n ...

  6. C++中全排列算法函数next_permutation的使用方法

    首先,先看对next_permutation函数的解释: http://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_ ...

  7. c++中的全排列函数next_permutation()

    全排列函数next_permutation() prev_permutation函数(按降序排序) 计算序列全排列的函数:next_permutation(start,end),此函数求的是当前排列的 ...

  8. C++:全排列函数next_permutation()和prev_permutation()

    文章目录 前言 一.next_permutation()用法 简单使用 例子1 第m个最小的数字序列 自定义排序 大小写字母排序 二.prev_permutation()用法 三.STL next_p ...

  9. 【C++】next_permutation / prev_permutation函数

    关于next_permutation函数 next_permutation和prev_permutation函数都是C++STL中的全排列函数. 函数原型: #include < algorit ...

最新文章

  1. Java快速教程--vamei 学习笔记(基础篇)
  2. 如何提升研发人员的非技术才能
  3. mysql常用的约束_MySQL常用的约束条件
  4. echarts解决自适应图表被压缩问题
  5. 批量管理Linux服务器,命令行工具Omnitty
  6. java 抽象类语法_JAVA基础语法8--多态/抽象类/抽象方法
  7. linux刻录win10u盘_手把手教你装系统之【制作官方win10安装U盘】
  8. 程序员结婚晚回家怕出轨吗?老婆:不怕,老公是写代码的,忙得很!
  9. 又学到了一个拒绝加班的技巧
  10. Web前端开发:SQL Jsp小项目(一)
  11. 股票的科创板,新三板,创业板到底哪个能让你赚钱
  12. mysql+同步大师,mysql同步异常 - 低调的糊涂虫的个人页面 - OSCHINA - 中文开源技术交流社区...
  13. 从数据表中取出第n条到第m条的记录的方法
  14. C#学习笔记——类的继承
  15. AIX系统修改文件系统的方法
  16. python识别图片文字、并返回文字坐标_简易OCR图片文字识别工具的进一步改进(增加显示图片的功能)...
  17. mysql concat字符串拼接函数使用
  18. ckplayer php,ckplayer播放器
  19. 【noip模拟题】天神下凡(贪心)
  20. linux 子程序返回错误代码,execvp:在程序中调子程序并获取返回值

热门文章

  1. 【Flutter】监听滚动动作 控制组件 透明度渐变 ( 移除顶部状态栏空白 | 帧布局组件 | 透明度组件 | 监听滚动组件 )
  2. 【Android 内存优化】Bitmap 内存占用计算 ( Bitmap 图片内存占用分析 | Bitmap 内存占用计算 | Bitmap 不同像素密度间的转换 )
  3. jQuery学习随笔(一)
  4. 今天做了一个复杂的table
  5. OpenGL初学时遇到的一些常见的函数之解析
  6. su: cannot set user id: Resource temporarily unavailable
  7. 使用java底层实现邮件的发送(含测试,源码)
  8. 算法笔记_156:算法提高 6-17复数四则运算(Java)
  9. MySQL5.6多实例部署
  10. 网站商务通如何导出查看历史聊天纪录