C++STL 之排列

固然我们可以自己使用递归编写全排列程序,但是既然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函数

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     string str;
 8     cin>>str;
 9     int len=str.length();
10     char *cstr=(char *)str.c_str();
11     cout<<"排列输出如下"<<endl;
12     do
13     {
14         cout<<cstr<<endl;
15     }while(next_permutation(cstr,cstr+len));
16     cout<<"排列之后cstr变为:"<<endl;
17     cout<<cstr;
18     return 0;
19 }

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

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     string str;
 8     cin>>str;
 9     int len=str.length();
10     char *cstr=(char *)str.c_str();
11     sort(cstr,cstr+len);
12     cout<<"排列输出如下"<<endl;
13     do
14     {
15         cout<<cstr<<endl;
16     }while(next_permutation(cstr,cstr+len));
17     cout<<"排列之后cstr变为:"<<endl;
18     cout<<cstr;
19     return 0;
20 }

加上排序之后,看看效果

时间: 2025-01-18 00:28:02

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

STL next_permutation排列

概念 全排列的生成算法有很多种,有递归遍例,也有循环移位法等等.C++/STL中定义的next_permutation和prev_permutation函数则是非常灵活且高效的一种方法,它被广泛的应用于为指定序列生成不同的排列.本文将详细的介绍prev_permutation函数的内部算法. 按照STL文档的描述,next_permutation函数将按字母表顺序生成给定序列的下一个较大的序列,直到整个序列为减序为止.prev_permutation函数与之相反,是生成给定序列的上一个较小的序列

STL的排列函数及字符串的排序方法

next_permutation(a,a+n);  a代表数组的头地址,a+n代表数组的长度. 运用该函数,a数组将变成原排列的下一个排列. 与之相反的函数为prev_permutation(a,a+n); #include <bits/stdc++.h> using namespace std; typedef long long LL; int f(int n) { int sum = 1; for(int i = 2; i <= n; i++) sum *= i; return s

STL练习-排列2

Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束. Output 对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔.每组输出数据间空一行,最后一组数据后面没有空行. Sample Input 1 2 3 4 1 1 2 3 0 1 2 3 0 0 0 0

POJ-2718

Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 3306 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in

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

擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长.现在需要你写一个程序来验证擅长排列的小明到底对不对. 输入 第一行输入整数N(1<N<10)表示多少组测试数据, 每组测试数据第一行两个整数 n m (1<n<9,0<m<=n) 输出 在1-n中选

stl 之 next_permutation 求出一个排序的下一个排列的函数 转载

这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<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]<<

枚举所有排列-STL中的next_permutation

枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 #include<cstdio> #include<algorithm> using namespace std; int main() { int n,p[10]; scanf("%d",&n); for(int i=0;i<n;i++) scan

nyoj19擅长排列的小明(DFS或STL)

擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长.现在需要你写一个程序来验证擅长排列的小明到底对不对. 输入 第一行输入整数N(1<N<10)表示多少组测试数据, 每组测试数据第一行两个整数 n m (1<n<9,0<m<=n) 输出 在1-n中选

排列字母stl

#include<iostream> #include<string> #include<vector> //vector的头文件 #include<algorithm> //next_permutation prev_permutation的头文件,排列无重复 using namespace std; int main() { string s; vector<string> v; //容器内存有大小,以及容量 int x,y,z,q; int