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

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

逆转元素次序

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()的用法

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll,"coll: ");
reverse(coll.begin(),coll.end());
PRINT_ELEMENTS(coll,"coll: ");
reverse(coll.begin()+,coll.end()-);
PRINT_ELEMENTS(coll,"coll: ");
reverse_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}

旋转(Rotating)元素次序

1.旋转序列内的元素

void

rotate(ForwardIterator beg,ForwardIterator newBeg,

ForwardIterator end)

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

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

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

 #include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll,"coll: ");
rotate(coll.begin(),coll.begin()+,coll.end());
PRINT_ELEMENTS(coll,"one left: ");
rotate(coll.begin(),coll.end()-,coll.end());
PRINT_ELEMENTS(coll,"two right: ");
rotate(coll.begin(),find(coll.begin(),coll.end(),),coll.end());
PRINT_ELEMENTS(coll,"4 first: ");
}

2.复制并同时旋转元素

OutputIterator

rotate_copy(ForwardIterator sourceBeg,ForwardIterator newBeg,

ForwardIterator sourceEnd,

OutputIterator destBeg)

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

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

 #include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
set<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll);
set<int>::iterator pos=coll.begin();
advance(pos,);
rotate_copy(coll.begin(),pos,coll.end(),ostream_iterator<int>(cout," "));
cout<<endl;
pos=coll.end();
advance(pos,-);
rotate_copy(coll.begin(),pos,coll.end(),ostream_iterator<int>(cout," "));
cout<<endl;
rotate_copy(coll.begin(),coll.find(),coll.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}

排列元素(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()将所有元素的所有可能排列的过程

 #include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll,"on entry: ");
while(next_permutation(coll.begin(),coll.end()))
PRINT_ELEMENTS(coll," ");
PRINT_ELEMENTS(coll,"afterward: ");
while(prev_permutation(coll.begin(),coll.end()))
PRINT_ELEMENTS(coll," ");
PRINT_ELEMENTS(coll,"now: ");
while(prev_permutation(coll.begin(),coll.end()))
PRINT_ELEMENTS(coll," ");
PRINT_ELEMENTS(coll,"afterward: ");
}

重排元素(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()来打乱元素次序

 #include <cstdlib>
#include "algostuff.hpp"
using namespace std; class MyRandom
{
public:
ptrdiff_t operator()(ptrdiff_t max)
{
double tmp;
tmp=static_cast<double>(rand())/static_cast<double>(RAND_MAX);
return static_cast<ptrdiff_t>(tmp * max);
}
}; int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll,"coll: ");
random_shuffle(coll.begin(),coll.end());
PRINT_ELEMENTS(coll,"shuffled: ");
sort(coll.begin(),coll.end());
PRINT_ELEMENTS(coll,"sorted: ");
MyRandom rd;
random_shuffle(coll.begin(),coll.end(),rd);
PRINT_ELEMENTS(coll,"shuffled: ");
}

将元素向前搬移

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()的用法以及两者的区别

 #include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll1;
vector<int> coll2;
INSERT_ELEMENTS(coll1,,);
INSERT_ELEMENTS(coll2,,);
PRINT_ELEMENTS(coll1,"coll1: ");
PRINT_ELEMENTS(coll2,"coll2: ");
cout<<endl;
vector<int>::iterator pos1,pos2;
pos1=partition(coll1.begin(),coll1.end(),not1(bind2nd(modulus<int>(),)));
pos2=stable_partition(coll2.begin(),coll2.end(),not1(bind2nd(modulus<int>(),)));
PRINT_ELEMENTS(coll1,"coll1: ");
cout<<"first odd element: "<<*pos1<<endl;
PRINT_ELEMENTS(coll2,"coll1: ");
cout<<"first odd elements: "<<*pos2<<endl;
PRINT_ELEMENTS(coll2,"coll2: ");
}

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. 魔法使的烟花(NOIP模拟赛Round 7)

    [问题描述] 魔法森林里有很多蘑菇,魔法使常常采摘它们来制作魔法药水.为了在6月的那个奇妙的晚上用魔法绽放出最绚丽的烟花,魔法使决定对魔法森林进行一番彻底的勘探. 魔法森林分为n个区域,由n-1条长度 ...

  2. win8下notepad++无法设置文件关联

  3. 使用 ElasticSearch Aggregations 进行统计分析

    https://blog.csdn.net/zxjiayou1314/article/details/53837719/

  4. git从远程仓库中更新代码到本地仓库

    git从远程仓库中更新代码到本地仓库 有时候在使用git pull的时候,会莫名才报错.查了很多资料,尝试过git的很多命令.包括git fetch命令,都会报同样的错.最后终于发现了一条捷径,由网友 ...

  5. Tarjan缩点+LCA【p2783】有机化学之神偶尔会做作弊

    Description 你翻到那一题:给定一个烃,只含有单键(给初中生的一个理解性解释:就是一堆碳用横线连起来,横线都是单条的). 然后炎魔之王拉格纳罗斯用他的火焰净化了一切环(???).所有的环状碳 ...

  6. Ubuntu 16.04使用timedatectl进行管理时间(UTC/CST)(服务器/桌面)

    说明:16.04开始,systemd接管了系统之后就不再使用/etc/default/rcS和ntpdate.dpkg-reconfigure tzdata进行时间的管理,所以在这些地方设置是无效的, ...

  7. 微服务实施Spring Boot/Spring Cloud中踩过的坑(转)

    http://tietang.wang/2016/09/08/%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%AE%9E%E6%9 ...

  8. MathType requires a newer version of MT Extra等MathType问题的不兼容性解决方案

    常见问题解决方法: 1.MathType 6.0与office 2007兼容问题 由于Office软件安装时默认是不安装公式编辑器的,在安装完MathType 6.0之后,需要将\MathType 6 ...

  9. 手动删除SVCH0ST.EXE的方法

        最近几天在办公室的计算机上又发现了一种病毒,在进程管理器中多出了两个进程:SVCH0ST.EXE.IEXPLORE.EXE,经一番查看揭开了它们的真面目,现将清除这种病毒的方法总结如下: 病毒 ...

  10. nginx服务器设置path_info模式

    1.find / -name nginx.conf找到nginx配置文件 2. ## The default server#server { listen 80; #填写自己的域名 server_na ...