变序性算法改变元素的次序,但不改变元素值。

这些算法不能用于关联式容器,因为在关联式容器中,元素有一定的次序,不能随意变动。

逆转元素次序

void

reverse(BidirectionalIterator beg,BidirectionalIterator end)

OutputIterator

reverse_copy(BidirectionalIterator sourceBeg,BidirectionalIterator sourceEnd,

OutputIterator destBeg)

1.reverce()会将区间[beg,end)内的元素全部逆序

2.reverse_copy()是reverse()跟copy()的组合

下面这个程序展示reverse()和reverse_copy()的用法

  1. #include <iterator>
  2. #include "algostuff.hpp"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. vector<int> coll;
  8. INSERT_ELEMENTS(coll,,);
  9. PRINT_ELEMENTS(coll,"coll: ");
  10. reverse(coll.begin(),coll.end());
  11. PRINT_ELEMENTS(coll,"coll: ");
  12. reverse(coll.begin()+,coll.end()-);
  13. PRINT_ELEMENTS(coll,"coll: ");
  14. reverse_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
  15. cout<<endl;
  16. }

旋转(Rotating)元素次序

1.旋转序列内的元素

void

rotate(ForwardIterator beg,ForwardIterator newBeg,

ForwardIterator end)

1.将区间[beg,end)内的元素进行旋转,执行后*newBeg成为新的第一元素

2.调用者必须确保newBeg是[beg,end)内的一个有效位置,否则会引发未定义的行为

以下程序示范如何使用rotate()

  1. #include "algostuff.hpp"
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. vector<int> coll;
  7. INSERT_ELEMENTS(coll,,);
  8. PRINT_ELEMENTS(coll,"coll: ");
  9. rotate(coll.begin(),coll.begin()+,coll.end());
  10. PRINT_ELEMENTS(coll,"one left: ");
  11. rotate(coll.begin(),coll.end()-,coll.end());
  12. PRINT_ELEMENTS(coll,"two right: ");
  13. rotate(coll.begin(),find(coll.begin(),coll.end(),),coll.end());
  14. PRINT_ELEMENTS(coll,"4 first: ");
  15. }

2.复制并同时旋转元素

OutputIterator

rotate_copy(ForwardIterator sourceBeg,ForwardIterator newBeg,

ForwardIterator sourceEnd,

OutputIterator destBeg)

这是copy()和rotate()的组合

以下程序示范rotate_copy()的用法

  1. #include <iterator>
  2. #include "algostuff.hpp"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. set<int> coll;
  8. INSERT_ELEMENTS(coll,,);
  9. PRINT_ELEMENTS(coll);
  10. set<int>::iterator pos=coll.begin();
  11. advance(pos,);
  12. rotate_copy(coll.begin(),pos,coll.end(),ostream_iterator<int>(cout," "));
  13. cout<<endl;
  14. pos=coll.end();
  15. advance(pos,-);
  16. rotate_copy(coll.begin(),pos,coll.end(),ostream_iterator<int>(cout," "));
  17. cout<<endl;
  18. rotate_copy(coll.begin(),coll.find(),coll.end(),ostream_iterator<int>(cout," "));
  19. cout<<endl;
  20. }

排列元素(Permuting)元素

bool

next_permutation(BidirectionalIterator beg,

BidirectionalIterator end)

bool

prev_permutation(BidirectionalIterator beg,

BidirectionalIterator end)

1.next_permutation()会改变区间[beg,end)内的元素次序,使他们符合“下一个排列次序”

2.prev_permutation()会改变区间[beg,end)内的元素次序,是他们符合“上一个排列次序”

下面这个例子展示利用next_permutation()和prev_permutation()将所有元素的所有可能排列的过程

  1. #include "algostuff.hpp"
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. vector<int> coll;
  7. INSERT_ELEMENTS(coll,,);
  8. PRINT_ELEMENTS(coll,"on entry: ");
  9. while(next_permutation(coll.begin(),coll.end()))
  10. PRINT_ELEMENTS(coll," ");
  11. PRINT_ELEMENTS(coll,"afterward: ");
  12. while(prev_permutation(coll.begin(),coll.end()))
  13. PRINT_ELEMENTS(coll," ");
  14. PRINT_ELEMENTS(coll,"now: ");
  15. while(prev_permutation(coll.begin(),coll.end()))
  16. PRINT_ELEMENTS(coll," ");
  17. PRINT_ELEMENTS(coll,"afterward: ");
  18. }

重排元素(Shuffling,搅乱次序)

void

random_shuffle(RandomAccessIterator beg,RandomAccessIterator end)

void

random_shuffle(RandomAccessIterator beg,RandomAccessIterator end,

RandomFunc& op)

1.第一形式使用一个均匀分布随机数产生器来打乱区间[beg,end)内的元素次序

2.第二形式使用op打乱区间[beg,end)内的元素次序。

以下程序示范如何调用random_shuffle()来打乱元素次序

  1. #include <cstdlib>
  2. #include "algostuff.hpp"
  3. using namespace std;
  4.  
  5. class MyRandom
  6. {
  7. public:
  8. ptrdiff_t operator()(ptrdiff_t max)
  9. {
  10. double tmp;
  11. tmp=static_cast<double>(rand())/static_cast<double>(RAND_MAX);
  12. return static_cast<ptrdiff_t>(tmp * max);
  13. }
  14. };
  15.  
  16. int main()
  17. {
  18. vector<int> coll;
  19. INSERT_ELEMENTS(coll,,);
  20. PRINT_ELEMENTS(coll,"coll: ");
  21. random_shuffle(coll.begin(),coll.end());
  22. PRINT_ELEMENTS(coll,"shuffled: ");
  23. sort(coll.begin(),coll.end());
  24. PRINT_ELEMENTS(coll,"sorted: ");
  25. MyRandom rd;
  26. random_shuffle(coll.begin(),coll.end(),rd);
  27. PRINT_ELEMENTS(coll,"shuffled: ");
  28. }

将元素向前搬移

BidirectionalIterator

partition(BidirectionalIterator beg,

BidirectionalIterator end,

UnaryPredicate op)

BidirectionalIterator

stable_partition(BidirectionalIterator beg,

BidirectionalIterator end,

UnaryPredicate op)

1.这两种算法将区间[beg,end)中造成以下一元判断式:op(elem)结果为true的元素向前端移动

2.stable_partition()会保持元素之间的相对次序。

以下程序示范partition()和stable_partition()的用法以及两者的区别

  1. #include "algostuff.hpp"
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. vector<int> coll1;
  7. vector<int> coll2;
  8. INSERT_ELEMENTS(coll1,,);
  9. INSERT_ELEMENTS(coll2,,);
  10. PRINT_ELEMENTS(coll1,"coll1: ");
  11. PRINT_ELEMENTS(coll2,"coll2: ");
  12. cout<<endl;
  13. vector<int>::iterator pos1,pos2;
  14. pos1=partition(coll1.begin(),coll1.end(),not1(bind2nd(modulus<int>(),)));
  15. pos2=stable_partition(coll2.begin(),coll2.end(),not1(bind2nd(modulus<int>(),)));
  16. PRINT_ELEMENTS(coll1,"coll1: ");
  17. cout<<"first odd element: "<<*pos1<<endl;
  18. PRINT_ELEMENTS(coll2,"coll1: ");
  19. cout<<"first odd elements: "<<*pos2<<endl;
  20. PRINT_ELEMENTS(coll2,"coll2: ");
  21. }

STL学习笔记(变序性算法)的更多相关文章

  1. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  2. STL学习笔记7 ---- algorithm(算法)

    STL中算可以分为三种, 1.变序型队列算法,可以改变容器内的数据: 2.非变序型队列算法,处理容器内的数据而不改变他们 : 3.通用数值算法,这涉及到很多专业领域的算术操作,这里不做介绍. 第一是变 ...

  3. STL学习笔记(非变动性算法)

    辅助函数 本节跟以后几节将对所有STL算法逐一详细讨论.为了简化这些例子,我们使用了一些辅助函数,分别用于对容器进行输出跟插入操作. #ifndef ALGOSTUFF_HPP #define ALG ...

  4. STL学习笔记(算法概述)

    算法头文件 要运用C++标准程序库的算法,首先必须包含头文件<algorithm> 使用STL算法时,经常需要用到仿函数以及函数配接器.它们定义域<functional>头文件 ...

  5. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  6. Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value

    Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...

  7. 机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析

    机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析 关键字:Apriori.关联规则挖掘.频繁项集作者:米仓山下时间:2018 ...

  8. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  9. Effective STL 学习笔记 Item 30: 保证目标区间足够大

    Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...

随机推荐

  1. hihoCoder挑战赛34 B题(快速求第k轮冒泡排序的结果)

    官方题解:https://media.hihocoder.com/contests/challenge34/tutorials-previewed.pdf 题目链接:http://hihocoder. ...

  2. 关于CI框架加入sphinx官方API接口文件的时候,需要注意的问题

    从sphinx下载的官方文件sphinxapi.php中类名为class SphinxClient 加入到CI框架,放在system/libraries/下,由于需要遵从CI框架libraries类名 ...

  3. Python基础-列表、元祖

    1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 names = ['Alex',"Tenglan",'Eric ...

  4. hdu 5084(矩阵操作)

    HeHe Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  5. MVC中AuthorizeAttribute用法并实现授权管理

    1.创建一个类(用来检查用户是否登录和用户权限)代码如下 public class MemberCheckAttribute : AuthorizeAttribute { //AuthorizeAtt ...

  6. SYSPROCESSES 查看连接

    原文:SYSPROCESSES 查看连接 SELECT at.text,sp.* FROM[Master].[dbo].[SYSPROCESSES] sp CROSS APPLY sys.dm_exe ...

  7. Delphi创建开机启动项的方法示例

    Delphi可以通过创建开机启动项键值的方法,将程序添加到开机启动项中.通过本实例代码就可以为您的程序添加到快速启动中,随着Windows一起启动,开机即运行的程序.该实例代码简单,主要是通过添加注册 ...

  8. andriod绘制图形

    使用view画图,有两个重要的组件需要介绍: (1)Paint 可以理解为画刷或者画笔,去主要用来设置绘图使用的颜色.填充方式.透明度.字体以及字体样式等. (2)Canvas 画布,在view上显示 ...

  9. JAVA常见算法题(十九)

    package com.xiaowu.demo; /** * * 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. * * * @author WQ ...

  10. 基于Prometheus,Alermanager实现Kubernetes自动伸缩

    到目前为止Kubernetes对基于cpu使用率的水平pod自动伸缩支持比较良好,但根据自定义metrics的HPA支持并不完善,并且使用起来也不方便. 下面介绍一个基于Prometheus和Aler ...