C++STL中全排列函数next_permutation的使用
来源:互联网 发布:while的用法c语言 编辑:程序博客网 时间:2024/05/16 05:21
next_permutation函数
> 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_permutation(start,end),和prev_permutation(start,end)。这两个函数作用是一样的,区别就在于前者求的是当前排列的下一个排列,后一个求的是当前排列的上一个排列。至于这里的“前一个”和“后一个”,我们可以把它理解为序列的字典序的前后,严格来讲,就是对于当前序列pn,他的下一个序列pn+1满足:不存在另外的序列pm,使pn<pm<pn+1.
对于next_permutation函数,其函数原型为:
#include <algorithm> bool next_permutation(iterator start,iterator end)
当当前序列不存在下一个排列时,函数返回false,否则返回true
我们来看下面这个例子:
#include <iostream> #include <algorithm> using namespace std; int main() { int num[3]={1,2,3}; do { cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl; }while(next_permutation(num,num+3)); return 0; }
输出结果为(排序完成的标志是所有数实现倒序排列):
当我们把while(next_permutation(num,num+3))中的3改为2时,输出就变为了(排序完成的标志是前两个数实现倒序排列):
由此可以看出,next_permutation(num,num+n)函数是对数组num中的前n个元素进行全排列,同时并改变num数组的值。
另外,需要强调的是,next_permutation()在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。比如,如果数组num初始化为2,3,1,那么输出就变为了:
此外,next_permutation(node,node+n,cmp)可以对结构体num按照自定义的排序方式cmp进行排序。
阅读全文
0 0
- C++STL中全排列函数next_permutation的使用
- C++STL中全排列函数next_permutation的使用
- C++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++)
- STL中全排列函数 next_permutation
- 使用STL的next_permutation函数生成全排列
- 全排列函数next_permutation在STL的使用
- poj 1833 排列 [ 使用STL的next_permutation函数生成全排列 + STL中copy函数的用法完美解决 ]
- STL 中 next_permutation 的全排列思路
- POJ 1833:排列 [#使用STL的next_permutation函数生成全排列#]
- 全排列(STL之next_permutation函数的应用)
- 模拟版本更新
- [SDUT](2053)数据结构实验之链表九:双向链表 ---双向链表(线性表)
- Java之颜色工具-yellowcong
- thoughtworks面试题
- java中char输入没有nextChar问题
- C++STL中全排列函数next_permutation的使用
- eclipsectrl+alt+上下键屏幕颠倒解决方案
- Elasticsaech-head插件安装 Elasticsearch5.X
- 树的高度
- ntp时间服务同步
- 解决Ajax 跨域问题
- HDU 5882 Balanced Game 分析+欧拉图
- LRU Cache leetcode
- Android生命周期详解