本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

next_permutation

-----------------------------------------------------------------------



描写叙述: 取得 [first, last) 所标示之序列的下一个排列组合。假设没有,返回 false,有,返回true

思路:

从后往前

1.找两个相邻元素,令左端的元素为*i,右端的元素为*ii,且满足 *i < *ii

2.找出第一个大于 *i 的元素,令其为 *j,将*i,*j元素对调

3.将ii右端的全部元素颠倒

源代码:

template <class BidirectionalIterator>
bool next_permutation(BidirectionalIterator first,
BidirectionalIterator last) {
if (first == last) return false;
BidirectionalIterator i = first;
++i;
if (i == last) return false;
i = last;
--i; for(;;) {
BidirectionalIterator ii = i;
--i;
if (*i < *ii) { //满足 *i < *ii
BidirectionalIterator j = last;
while (!(*i < *--j)); // 找到满足 *i < *j 的 *j
iter_swap(i, j);
reverse(ii, last); //将 ii右端的全部元素颠倒
return true;
}
if (i == first) {
reverse(first, last);//已经是最后一个排列了,逆向重排回到第一个排列
return false;
}
}
}

演示样例:

int main()
{
int a[] = {0,1,2,3,4};
int n = sizeof(a)/sizeof(int);
do{
copy(a, a + n, ostream_iterator<int>(cout, " "));
cout << endl;
}while(next_permutation(a, a + n));
}

STL 源代码剖析 算法 stl_algo.h -- next_permutation的更多相关文章

  1. STL 源代码剖析 算法 stl_algo.h -- search

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie search --------------------------------------- ...

  2. STL 源代码剖析 算法 stl_algo.h -- partial_sort / partial_sort_copy

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partial_sort / partial_sort_copy ------------- ...

  3. STL 源代码剖析 算法 stl_algo.h -- lower_bound

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie lower_bound(应用于有序区间) ------------------------- ...

  4. STL 源代码剖析 算法 stl_algo.h -- random_shuffle

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie random_shuffle ------------------------------- ...

  5. STL 源代码剖析 算法 stl_algo.h -- merge sort

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...

  6. STL 源代码剖析 算法 stl_algo.h -- partition

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partition ------------------------------------ ...

  7. STL 源代码剖析 算法 stl_algo.h -- equal_range

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie equal_range(应用于有序区间) ------------------------- ...

  8. STL 源代码剖析 算法 stl_algo.h -- inplace_merge

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie inplace_merge(应用于有序区间) ----------------------- ...

  9. STL 源代码剖析 算法 stl_algo.h -- nth_element

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie nth_element ---------------------------------- ...

随机推荐

  1. AC日记——小A的糖果 洛谷七月月赛

    小A的糖果 思路: for循环贪心: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defi ...

  2. Spring注解@Scope("prototype")

    spring 默认scope 是单例模式 这样只会创建一个Action对象 每次访问都是同一个Action对象,数据不安全 struts2 是要求 每次次访问 都对应不同的Action scope=& ...

  3. zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)

    题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...

  4. gvim 编辑器配置

    "关才兼容模式 set nocompatible "模仿快捷键,如:ctrt+A 全选.Ctrl+C复制. Ctrl+V 粘贴等 source $VIMRUNTIME/vimrc_ ...

  5. 最全python面试题

    Python语言特性 1 Python的函数参数传递 看两个例子: a = 1 def fun(a): a = 2 fun(a) print a # 1 a = [] def fun(a): a.ap ...

  6. XPath语法和CSS选择器介绍

    XPath语法 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历.XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 ...

  7. Flask实战第44天:完成前台注册功能

    注册功能后端逻辑 用户注册要把注册的表单提交上来,因此,我要先对表单进行验证,编辑front.forms from apps.forms import BaseForm from wtforms im ...

  8. 使用NGUI来制作技能的CD冷却效果

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class CDScript ...

  9. Ubuntu用户管理原理

    Ubuntu账户: Ubuntu有三类账户:超级用户.普通用户以及系统用户. 每一个用户在ubuntu中都必须拥有一种账户,在Ubuntu中, /etc/passwd用来保存每个账户的信息.实际密码保 ...

  10. UML对象图、包图

    对象图(Object Diagram)显示了一组对象和他们之间的关系.使用对象图阿狸说明数据结构,类图中的类或组件等实例的快照.对象图和类图一样,反应了系统的静态过程,但它是以实际的或原型化为基础来表 ...