next_permutation()—遍历全排列
next_permutation()--遍历全排列
template <class BidirectionalIterator>
bool next_permutation (BidirectionalIterator first,
BidirectionalIterator last);
template <class BidirectionalIterator, class Compare>
bool next_permutation (BidirectionalIterator first,
BidirectionalIterator last, Compare comp);
如果可以继续遍历则返回True,否则返回False,直到数组从增序一直遍历到减序.
示例:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
char arr[] = {'A', 'B', 'C'};
// int arr[] = {1, 2, 3};
// string arr[] = {"A", "B", "C"};
sort(arr, arr+3);
// 排序保证数组顺序为增序,这样才可以把全排列遍历完.
do {
cout << arr[0] << arr[1] << arr[2] << endl;
} while (next_permutation(arr, arr+3));
cout << "After Loop: " << arr[0] << arr[1] << arr[2] << endl;
return 0;
}
结果:
ABC
ACB
BAC
BCA
CAB
CBA
After Loop: ABC
next_permutation()—遍历全排列的更多相关文章
- next_permutation(全排列)
废话不多说,直接上代码,谁测试,谁知道 C++: #include<bits/stdc++.h> using namespace std; typedef long long ll; in ...
- (转)ACM next_permutation函数
转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记 (1) int 类型的next_permuta ...
- next_permutation函数
这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记 与之完全相反的函数还有prev_permutation (1) int 类 ...
- buaaoj230——next_permutation的应用
题目地址 简单的全排列输出,借用stl中的next_permutation就非常简单了. 关于next_permutation:(备忘,来源网络) /*这是一个求一个排序的下一个排列的函数,可以遍历全 ...
- next_permutation()函数 和 prev_permutation() 按字典序求全排列
next_permutation功能: 求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm> 与之完全相反的函数还有prev_permutation 这个 ...
- HDOJ 1716 排列2 next_permutation函数
Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡 ...
- HDOJ 1716 排列2(next_permutation函数)
Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡 ...
- NYOJ--STL--擅长排列的小明(强大的string :: iterator 和next_permutation)
NYOJ--STL--擅长排列的小明 #include <iostream> #include <string> #include <algorithm> usin ...
- STL next_permutation和prev_permutation函数
利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置, 直接求到第一个不按升序排列的序列. #include <iostream> #include & ...
随机推荐
- Can jxta be used to develop online card game (p2p style)?
Can jxta be used to develop online card game (p2p style)? https://www.java.net//node/677134 I am new ...
- Linux性能监测
1.Linux性能监测:监测目的与工具介绍 看了某某教程.读了某某手册,按照要求改改某些设置.系统设定.内核参数就认为做到系统优化的想法很傻很天真:)系统优化是一项复杂.繁琐.长期的工作,优化前需要监 ...
- CAAnimation解读
序言 CAAnimation是一个抽象类,遵循了CAMediaTiming协议和CAAction协议!我们不要直接使用CAAnimation类,而是使用其子类: CATransition:提供渐变效果 ...
- lib 和 dll 的区别、生成以及使用详解
首先介绍一下静态库(静态链接库).动态库(动态链接库)的概念,首先两者都是代码共享的方式. 静态库:在链接步骤中,连接器将从库文件取得所需的代码,复制到生成的可执行文件中,这种库称为静态库,其特点是可 ...
- Unity3D之Mecanim动画系统学习笔记(十):Mecanim动画的资源加载相关
资源加载是必备的知识点,这里就说说Mecanim动画的资源如何打包及加载. 注意,Unity4.x和Unity5.x的AssetBundle打包策略不一样,本笔记是基于Unity4.x的AssetBu ...
- mac ide
常用IDE xcode sublime text eclipse xampp + phpstorm sql客户端:sequel pro 虚拟机:parallels desktop sftp客户端:Cy ...
- Invoke-Express 执行多个批处理命令的函数
function Mult_ping ($ips) { # $cmdline = "PIng" foreach ($ip in $ips) { $cmdline = "p ...
- 数据挖掘十大经典算法(9) 朴素贝叶斯分类器 Naive Bayes
贝叶斯分类器 贝叶斯分类器的分类原理是通过某对象的先验概率,利用贝叶斯公式计算出其后验概率,即该对象属于某一类的概率,选择具有最大后验概率的类作为该对象所属的类.眼下研究较多的贝叶斯分类器主要有四种, ...
- [置顶] ASP.NET MVC - Model Binding
Http Request 到Input Model的绑定按照model的类型可分为四种情况. Primitive type Collection of primitive type Complex t ...
- oc-08-内存分析
说有对象公用类的一个方法