STL之next_permutation函数对各种类型的全排列实例
来源:互联网 发布:网络上小姐姐表示什么 编辑:程序博客网 时间:2024/06/05 17:36
最近学了个next_permutation函数,但学习的还不是很透彻,这里转载一篇博文详解next_permutation函数对各种变量类型的全排列。(转载地址:点击打开链接)
若需要了解next_permutation函数基本含义或c源码,请点击:点击打开链接
这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>
与之完全相反的函数还有prev_permutation
(1) int 类型的next_permutation
int main()
{
a[0]=1;a[1]=2;a[2]=3;
{
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
} while (next_permutation(a,a+3)); //参数3指的是要进行排列的长度
//如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继
}
输出:
如果改成 while(next_permutation(a,a+2));
则输出:
只对前两个元素进行字典排序
显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3
若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
next_permutation(list,list+3);
cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;
//输出: 1 2 3
(2) char 类型的next_permutation
int main()
{
cin >> ch;
sort(ch, ch + strlen(ch) );
//该语句对输入的数组进行字典升序排序。如输入9874563102 cout<<ch; 将输出0123456789,这样就能输出全排列了
cout<< ch << endl;
}while(next_permutation(first, last));
}
//这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序
//若采用 while(next_permutation(ch,ch+5)); 如果只输入1562,就会产生错误,因为ch中第五个元素指向未知
//若要整个字符串进行排序,参数5指的是数组的长度,不含结束符
(3) string 类型的next_permutation
int main()
{
{
cout<<line<<endl;
}
}
int main()
{
{
sort(line.begin(),line.end());//全排列
cout<<line<<endl;
cout<<line<<endl;
}
}
#include<iostream> //poj 1256 Anagram
#include<string>
#include<algorithm>
using namespace std;
int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.
{
}
int main()
{
cin>>n;
{
scanf("%s",ch);
sort(ch,ch+strlen(ch),cmp);
{
printf("%s\n",ch);
}while(next_permutation(ch,ch+strlen(ch),cmp));
}
}
- STL之next_permutation函数对各种类型的全排列实例
- STL之next_permutation函数对各种类型的全排列实例
- STL之next_permutation() 全排列函数
- 全排列(STL之next_permutation函数的应用)
- STL之 next_permutation函数{(全排列)按字典序!!!!!}
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- C++STL中全排列函数next_permutation的使用
- 使用STL的next_permutation函数生成全排列
- 全排列函数next_permutation在STL的使用
- C++STL中全排列函数next_permutation的使用
- C++STL中全排列函数next_permutation的使用
- php storm 设置字体 theme
- 代码重构方向原则指导
- 画橡皮线
- JAVA 文件操作
- 重置ArcMap
- STL之next_permutation函数对各种类型的全排列实例
- CentOS 6.3下PostgreSQL 的安装与配置
- [js实例] js判断浏览器是android还是ios还是微信浏览器
- MySQL控制台登录
- 设计模式-享元模式
- 画弧线
- 010 Comparison
- Geode分布式内存数据库rest api 之PHP版本
- 轻轻放下和不容侵犯